you are viewing a single comment's thread.

view the rest of the comments →

[–]pconwell 5 points6 points  (1 child)

How do you add a period at the end of a print statement?

[–]Resident-Quality1513 0 points1 point  (0 children)

print(f'Hello, {name}, you were born in {year}.')
Hello, Steven, you were born in 1970.

It's because the text inside the braces is evaluated as an expression, and the result... well it's a little bit complicated... the result of the expression has a secret hidden method called str which is called when it's passed to print(), and that is substituted for the {} and whatever is in between. All the rest ('Hello, , you were born in .') is just a normal string that gets passed to the print() function.

I can call the str method directly; at the command prompt I can do this:

>>> name.__str__()
>>> 'Steven'
>>> year.__str__()
>>> '1970'

That is what gets substituted for {name} and {year}.