Python string party

Python string party

For quite a while now there has been a better way to handle strings in Python. But for some reason it isn't used as much as I believe it should be.

Suppose you have the following variables:

name = "andy"
age = 42
likes = "Python"

and you want to generate a string that says:
"We know someone called andy who is aged 42. This person likes to use Python to program."


It's now simple and elegant to do that using f-Strings.

print(f"We know someone called {name} who is aged {age}")
print(f"This person likes to use {language} to program.")

That's it. No type conversion, concatenation, or hard to remember codes. Just start the string with f before the first ", and enclose all variables in {}.

It works anywhere you need to process a string. Give it a try!

Also, try something like this and see how useful it can be:

print(f"{name=}"  # helpful when quickly needing to see a variable

Have you used f strings? Will you now use them? Please let me know.