Formatting Strings in Python. How to use the format() method and... | by Luay Matalka | Towards Data Science

There are multiple ways to format a string in python. We will go over the format() method and f-strings to create the following:
We will use the first_name, last_name, and age variables to create a string that contains the first name, last name, and age of someone in the following format:
‘first_name last_name is age years old’
A sample sentence would be: ‘John Doe is 43 years old’.
One way to accomplish this would be to use the format method. The format() method is a string method. Once you call it off a string, it would replace all the curly brackets in the string with the values of the variables passed in the parenthesis, in the order they are passed in. Thus, the curly brackets act as placeholders for the variables.
For example, to use the format method for the above example, we would use the following code:
first_name = 'John'last_name = 'Doe'age = 43sentence = '{} {} is {} years old'.format(first_name, last_name, age)print(sentence)# 'John Doe is 43 years old'
Note: The format method will replace the curly brackets with the values of the variables in the order they appear within the parenthesis.
If we don’t want to worry about passing the variables in the correct order, we can use keys in each curly bracket and later assign the variable for that specific key:
first_name = 'John'last_name = 'Doe'age = 43sentence = '{first} {last} is {age_yrs} years old'.format(last=last_name, age_yrs=age, first=first_name)print(sentence)# 'John Doe is 43 years old'
Note how the variables don’t have to be passed in the correct order if we use specific keys for each placeholder.
Though the format() method does the job, there is a much more elegant way of formatting strings, and that’s by using f-strings.
towardsdatascience.com
F-strings are a new way to format strings in Python 3.6 and above. For most people, they are the preferred way to format strings since they are easy to read and thus much more intuitive.
f’{var_1} {var_2} is {var_3} years old’
To specify that we want to use an f-string, or formatted string, we just put an f infront of the string. Then we can directly add our variables into our curly brackets, in the location we want them to appear. Thus, we didn’t need to use any method or pass in any variables at the end of the string like we did with the format method. It is a much more intuitive way of formatting strings, since we don’t have to make sure that all the variables are in the same order as our placeholders to make sure they are being added in the correct location.
Thus, to accomplish the above task with f-strings, we would use the following code:
first_name = 'John'last_name = 'Doe'age = 43sentence = f'{first_name} {last_name} is {age} years old'print(sentence)# 'John Doe is 43 years old'
Similar to the format method, we can also run methods or functions within the f-string. For example, if we want to make the first name uppercase and last name lowercase, we can do so with the corresponding string methods within the f-string:
first_name = 'John'last_name = 'Doe'age = 43sentence = f'{first_name.upper()} {last_name.lower()} is {age} years old'print(sentence)# 'JOHN doe is 43 years old'
towardsdatascience.com
In this tutorial, we quickly looked at ways to format a string in python. We first looked at the format() method, and then at the more elegant and intuitive f-strings. We then saw how we can use methods or functions within f-strings.