Record#
- Learn the format in print. The format is as follows:
- One way is to simply replace {} with the position of the variable, and write the variables in order in the final format.
- Another way is to write a string in {}, such as {name}, and use string = variable in the final format, such as name=name, so you don't need to consider the position of the variable.
- There is also an f-string syntax, which looks simpler. Just use f" at the beginning, and you can directly use {variable name} in the string.
- The f format can also control the alignment of the print: left =
<
, right =>
, center =^
. The number after the symbol means align how many characters, for example: <30, which means left align 30 characters. - format can not only be used after print, but also after assignment, it feels like a way to format text and variables.
- f format is the abbreviation of format. That is,
print(f"") = print("".format())
- chatgpt says:
format()
is a built-in function used to insert values into placeholders in a string. It provides a more flexible and dynamic way to create formatted strings. You can use curly braces{}
as placeholders, and then pass the corresponding values through theformat()
function. - Today's exercise is to create a response to the thoughts of the 30th day, and center align it.
CODE#
print("30 Days Down")
for i in range(1, 30):
r = input(f"Day {i}\n")
t = f"You thought Day {i} was "
print(f"{t: ^40}")
print(f"{r: ^40}")
print()