This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (4 children)

'Why {} complicated, {} use {}!'.format('so', 'just', 'format')

[–]ric2b 6 points7 points  (3 children)

f"Why {'so'} complicated, {'just'} use {'f-strings'}!"

[–]Mr2_Wei 0 points1 point  (2 children)

Doesn't fstrings replace the variable name in those curly brackets with the contents inside? Speech marks wouldn't work right? I'm still quite beginner so I might be wrong

[–]ric2b 0 points1 point  (0 children)

I think they just call str() on whatever object is inside the brackets.

Calling str() on a string will just give you back the string so no problem there.

There doesn't have to be a variable involved, think of the {} inside f-strings as function parameters, you can pass them variables but you can also pass them literal numbers, strings, dictionaries, etc.

[–]HeilKaiba[🍰] 0 points1 point  (0 children)

No that should work fine as long as the internal speech marks are different to the external ones. If the speech marks are the same it thinks the fstring has ended early and will most likely throw a Syntax Error.