-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw3school_pythonstringformatting.py
More file actions
63 lines (48 loc) · 1.87 KB
/
w3school_pythonstringformatting.py
File metadata and controls
63 lines (48 loc) · 1.87 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
# Python String Formatting
# To make sure a string will display as expected, we can format the result with the format() method.
# String format()
# The format() method allows you to format selected parts of a string. Sometimes there are parts of a text that you do not control, maybe they come from a database, or user input?
# To control such values, add placeholders (curly brackets {}) in the text, and run the values through the format() method:
# E.g. Add a placeholder where you want to display the price:
"""
price = 49
txt = "The price is {} dollars"
print(txt.format(price))
"""
# You can add parameters inside the curly brackets to specify how to convert the value:
# E.g. Format the price to be displayed as a number with 2 decimals:
"""
price = 49
txt = "The price is {:.2f} dollars"
print(txt.format(price))
"""
# Multiple values
# E.g. If you want to use more values, just add more values to the format() method 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))
"""
# Index numbers
# E.g. You can use index numbers (a number inside the curly brackets {0}) to be sure the values are placed in the correct placeholder:
"""
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
"""
name = "Ikenna"
age = 36
print("His name is {1}. {1} is {0} years old".format(age,name))
"""
# Named indexes
# E.g. You can use named indexes by entering a name inside the curly brackets {carname}, but then you must use names when pass the parameter values:
"""
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))
"""