#Day17 — Fastest way to format strings in Python

Rahul Banerjee
Apr 7 · 2 min read
Photo by Oskar Yildiz on Unsplash

There are 3 ways to print strings in Python

  • Using the % operator
  • Using format()
  • Using f strings

We have three functions

  • func1 uses the % operator
  • func2 uses format()
  • func3 uses f strings

We will use the timeit function to measure the time taken by each function

We call each function 100 times and calculate the average time taken for the function call. The average time is stored in a list.

Below are the comparisons of the 3 functions

As you can see, f strings are faster as compared to format() and the % operator.

if…..else with f strings

num = 2
print(f"{num} is an {'even' if num%2 ==0 else 'odd'} number")
num = 3
print(f"{num} is an {'even' if num%2 ==0 else 'odd'} number")

Aligning strings

  • Left-aligned: {variable :> number}
  • Center-aligned: {variable :^ number}
  • Right-aligned: {variable :< number}
text = "string"
print(f'{text:>20}')
print(f'{text:^10}')
print(f'{text:<30}')
'''
OUTPUT
string
string
string
'''

Round floating points

num =  20.012345783
print(f'{num:0.3f}')
'''
OUTPUT
20.012
'''

Pad Zeroes

x = 10
print(f'{x:0}') #10
print(f'{x:02}') #10
print(f'{x:03}') #010
print(f'{x:04}') # 0010

If you want to pad n zeroes to a y digit number, it should be

print(f'{x:0n+y}')

Originally published at https://www.realpythonproject.com on April 7, 2021.

Daily Programming Tips

Everything from SQL to Python

Sign up for Daily Programming

By Daily Programming Tips

Get notification when a new article is published Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Rahul Banerjee

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Daily Programming Tips

Daily content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Rahul Banerjee

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Daily Programming Tips

Daily content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store