geohernandez
Menu
  • HOME
  • ABOUT
  • CONTACT ME
  • WORK WITH GEO
    • Data Specialist
    • Speaker Events
    • Resume
  • English
    • English
    • EspaƱol
Menu

Using interpolated format strings in Python

Posted on December 5, 2022March 11, 2023 by geohernandez

The release of Python 3.6 came with exciting functionalities. I want to speak about the feature called interpolated format string – f-strings for short- through this quick post. This new feature allows us to have a more readable, concise and even faster way of formatting string, in short words: pythonic code.

One of the most interesting features associated with the f-string is the capacity to include expressions which can be solved. The Python syntax is:

1
f "{variable or expression here}"

As you can observe, it is simple compared to previous functions, and it is less prone to commit mistakes as can happen when you depend on the order of the variables such as string.format syntax. As I mentioned above, using F-strings also enables you to put a full Python expression within the
placeholder braces. See the following example:

Python
1
2
3
4
5
6
7
8
9
10
# Evaluating expressions internally
baseball_teams = [
('Arizona Diamondbacks',1),
('Detroit Tigers', 4),
('Cincinnati Reds', 10),
]
 
for i, (item, count) in enumerate(baseball_teams):
    f_string = f'Row {i+1}: {item.title():<20s} = {count}'
    print(f_string)

It is interesting to remark on the capacity to include a format function inside of placeholder braces too. In this example, we formatted the length of the variable .. more explanation. By default, f-string use __str__(). Nonetheless, you could use __repr__() which can be helpful in case you want to get the parable string representation of an object; you only need to include the conversion flag !r as in this example:

Python
1
2
3
4
5
key = 'variable'
value = 1.2345
 
f_string = f'{key !r:<10} = {value: .2f}'
print(f_string)

As in the previous example, you are able to apply a format to variables parsed, including the number of digits after the decimal, justification on the string, etc. You can find more details about it here.

Finally, the following example shows you the speed of both approaches. We are using a library called timeit, its usage is simple and consists in supplying a statement of the number of iterations to be executed.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import timeit
 
testcode_traditional = '''
name = "Octavio"
age = 24
'%s is %s.' % (name, age)
'''
 
testcode_fstring = '''
name = "Octavio"
age = 24
f'{name} is {age}.'
'''
 
print(timeit.timeit(stmt=testcode_traditional, number=10000))
print(timeit.timeit(stmt=testcode_fstring, number=10000))

Once you execute, please observe the time delta between both options and you will have an additional reason to start using f-string over any older approach. Last but not least, thanks to my colleague and large landowner Esteban (SRE rockstar) at Languagewire for sharing this syntax. Happy coding!

Category: Chronicles from the trenches, Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search for articles

Recent Posts

  • Quick Guide: BigQuery Service Account Setup Using gcloud
  • The Art of Data Modeling in AI times
  • Getting Started with Snowflake’s Snowpipe for Data Ingestion on Azure

Categories

  • Airflow (1)
  • Azure (6)
  • Azure DevOps (2)
  • Bash script (1)
  • Blog (1)
  • Cassandra (3)
  • Chronicles from the trenches (26)
  • Data Architecture (3)
  • Data Engineering (11)
  • DB optimization (2)
  • Events (2)
  • GIT (1)
  • MySQL (1)
  • Python (7)
  • Snowflake (3)
  • SQL Saturday (1)
  • SSIS (2)
  • T-SQL (5)
  • Uncategorized (2)

Archives

  • May 2025 (1)
  • March 2025 (1)
  • January 2025 (2)
  • October 2024 (1)
  • July 2024 (1)
  • May 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • August 2023 (1)
  • June 2023 (1)
  • December 2022 (1)
  • November 2022 (1)
  • July 2022 (1)
  • March 2022 (1)
  • September 2021 (1)
  • May 2021 (1)
  • March 2021 (1)
  • February 2021 (3)
  • December 2020 (1)
  • October 2020 (3)
  • September 2020 (1)
  • August 2020 (1)
  • January 2020 (1)
  • August 2019 (1)
  • July 2019 (1)
  • June 2019 (1)
  • May 2019 (1)
  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (3)
  • October 2018 (1)
  • September 2018 (1)
  • August 2018 (2)
© 2025 geohernandez | Powered by Minimalist Blog WordPress Theme