Chapter 11: String Formatting

In Python, there are three main methods for string formatting.

 

1. In the first type of formatting, we concatenate strings using the + symbol, as shown in the example below.

 

language = "Python"

website  = "Free Learning"

print("I'm learning " + language + " language from " + website + " website.")

Output: I'm learning Python language from Free Learning website.

 

2. In the second type, we use curly braces {} inside double quotes and the .format() method for formatting, as shown in the code. This formatting is available for python version 3 and higher.

 

language = "Python"

website  = "Free Learning"

print("I'm learning {} language from {} website.".format(language,website))

Output: I'm learning Python language from Free Learning website. 

 

3. In the third type, we use curly braces {} inside double quotes with an f prefix before the quotes for f-string formatting, as shown in the code. This formatting is available for python version 3.6 and higher.

 

language = "Python"

website  = "Free Learning"

print(f"I'm learning {language} language from {website} website.")

Output: I'm learning Python language from Free Learning website. 

 

You can use any of the string formatting, you like.