Record#
- Learn about the format in print. The format is as follows:
- One way is to replace the variable's position with {} and write the variables in order in the format at the end.
- Another way is to write a string in {}, such as {name}, and use string = variable in the format at the end, such as name=name. This way, you don't need to consider the position of the variable.
- There is also a f-string syntax, which looks simpler. Just use f" at the beginning, and use {variable name} directly 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.
- The f format is an abbreviation of format. That is,
print(f"") = print("".format())
- ChatGPT says:
format()
is a built-in function used to insert values into the 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 a 30-day reflection question 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()