Python Tricks #27 - f-strings Python3.6+

Words
0
Reading
0 min
Listen
Play
7y
# f-strings are flexible way to do string interpolation available in Python 3.6+

# The old way with "format":
user = "Jane Doe"
action = "buy"
log_message = 'User {} has logged in and did an action {}.'.format(
      user,
      action
)
print(log_message)
# User Jane Doe has logged in and did an action buy.

# Instead of "format" after v3.6 we can use more flexible and readable way to do the same thing:
user = "Jane Doe"
action = "buy"
log_message = f'User {user} has logged in and did an action {action}.'
print(log_message)
# User Jane Doe has logged in and did an action buy.
Python Tricks #27 - f-strings Python3.6+ | Ecency