-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-format.py
More file actions
77 lines (61 loc) · 1.98 KB
/
Copy pathstring-format.py
File metadata and controls
77 lines (61 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Create an f-string:
# txt = f"The price is 49 dollars"
# print(txt)
#Add a placeholder for the price variable:
# price = 59
# txt = f"The price is {price} dollars"
# print(txt)
#Display the price with 2 decimals:
# price = 59
# txt = f"The price is {price:.2f} dollars"
# print(txt)
#Display the value 95 with 2 decimals:
# txt = f"The price is {95:.2f} dollars"
# print(txt)
#Perform a math operation in the placeholder, and return the result:
# txt = f"The price is {20 * 59} dollars"
# print(txt)
#Add taxes before displaying the price:
# price = 59
# tax = 0.25
# txt = f"The price is {price + (price * tax)} dollars"
# print(txt)
#Return "Expensive" if the price is over 50, otherwise return "Cheap":
# price = 49
# txt = f"It is very {'Expensive' if price>50 else 'Cheap'}"
# print(txt)
#use the string method upper()to convert a value into upper case letters:
# fruit = "maheen"
# txt = f"Sham loves {fruit.upper()}"
# print(txt)
#Create a function that converts feet into meters:
# def myconverter(x):
# return x * 0.3048
# txt = f"The plane is flying at a {myconverter(30000)} meter altitude"
# print(txt)
#Use a comma as a thousand separator:
# price = 59000
# txt = f"The price is {price:,} dollars"
# print(txt)
#Add a placeholder where you want to display the price:
# price = 49
# txt = "The price is {} dollars"
# print(txt.format(price))
#And add more placeholders:
# quantity = 3
# itemno = 567
# price = 49
# myorder = "I want {} pieces of item number {} for {:.2f} dollars."
# print(myorder.format(quantity, itemno, price))
# quantity = 3
# itemno = 567
# price = 49
# myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
# print(myorder.format(quantity, itemno, price))
#Also, if you want to refer to the same value more than once, use the index number:
# age = 36
# name = "John"
# txt = "His name is {1}. {1} is {0} years old."
# print(txt.format(age, name))
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))