February 2024
Beginner
352 pages
8h 16m
English
We will now see how to do a more complicated formatting of a string. This code looks complex, but if you do your comments above each line and break each thing down to its parts, you’ll understand it.
Listing 8.1: ex8.py
1 formatter = "{} {} {} {}"
2
3 print(formatter.format(1, 2, 3, 4))
4 print(formatter.format("one", "two", "three", "four"))
5 print(formatter.format(True, False, False, True))
6 print(formatter.format(formatter, formatter, formatter, formatter))
7 print(formatter.format(
8 "Try your",
9 "Own text here",
10 "Maybe a poem",
11 "Or a song about fear"
12 ))
1 1 2 3 4 2 one two three four 3 True False False True ...Read now
Unlock full access