DEV Community

Cover image for Formatted String Literals in Python
Sweta Shaw
Sweta Shaw

Posted on

Formatted String Literals in Python

Old-school way of formatting strings str.format()

The Python string .format() method was introduced in version 2.6. Following is an example that uses .format() method of formatting strings.

#Example 1 (simple)
name = "Sweta Shaw"
print("My name is {}".format(name))

#output
My name is Sweta Shaw
Enter fullscreen mode Exit fullscreen mode

The above code looks neat but the real struggle happens when the number of variables increases.

#Example 2 (complex)
first_name = "Sweta"
last_name = "Shaw"
profession = "software developer"
platform = "hashnode"

print("Hi! I am {first_name} {last_name}, a {profession} by profession currently writing this article on {platform}.".format(first_name= first_name, last_name =last_name , profession = profession, platform = platform))

# output
Hi! I am Sweta Shaw, a software developer by profession currently writing this article on hashnode.
Enter fullscreen mode Exit fullscreen mode

This is where the f-string literals come to the rescue. It was introduced in Python 3.6

A formatted string literal *or **f-string* is a string literal that is prefixed with f or F and curly braces containing expressions that will be replaced with their values.

# Example 1 using f-string
name = "Sweta"
print(f"My name is {name}")

# output
My name is Sweta Shaw
Enter fullscreen mode Exit fullscreen mode

Let's see how we can write the complex example 2 using f-strings:

# Example 2 using f-string
first_name = "Sweta"
last_name = "Shaw"
profession = "software developer"
platform = "hashnode"

print(f"Hi! I am {first_name} {last_name}, a {profession} by profession currently writing this article on {platform}.")

# output
Hi! I am Sweta Shaw, a software developer by profession currently writing this article on hashnode.
Enter fullscreen mode Exit fullscreen mode

We can also perform arithmetic operations using f-string literals.

f"{2 * 37}"
# output 
'74'
Enter fullscreen mode Exit fullscreen mode
a = 20
b = 50

print(f"The smaller number between {a} and {b} is {a if a < b else b}")

# output
The smaller number between 20 and 50 is 20
Enter fullscreen mode Exit fullscreen mode

Printing a list

authors = [("id", "author", "book"), ("101", "Changing India", "Dr. Manmohan Singh"), ("102","War and Peace", "Leo Tolstoy "), ("103", "Emma", "Jane Austen")]
for id, author,book in authors:
    print(f"{id} {author} {book}")

# output
id author book
101 Changing India Dr. Manmohan Singh
102 War and Peace Leo Tolstoy 
103 Emma Jane Austen

Enter fullscreen mode Exit fullscreen mode

Beautify this output using f-string indentation

This is done by mentioning the number of spaces taken by each variable. Here 'id' has been assigned 5 spaces and author and book 20 spaces each.

authors = [("id", "author", "book"), ("101", "Changing India", "Dr. Manmohan Singh"), ("102","War and Peace", "Leo Tolstoy "), ("103", "Emma", "Jane Austen")]
for id, author,book in authors:
    print(f"{id:{5}} {author:{20}} {book}")

# output:
id    author               book
101   Changing India       Dr. Manmohan Singh
102   War and Peace        Leo Tolstoy 
103   Emma                 Jane Austen


Enter fullscreen mode Exit fullscreen mode

You can read more about f-string literals here

Top comments (0)