The Complete Guide to Python f-Strings (2024)

Python f-strings provide an improved method for formatting strings and interpolating values inside of them.

While f-strings are powerful, they are also quite new. Python f-strings were only introduced in Python 3.6

Fortunately, I've got you covered. This tutorial will teach you everything you need to know about using f-strings in Python.

Introduction to Python f-strings

Python f-Strings are an improved method for formatting strings in python. It was introduced in Python 3.6 as a concise, faster, more readable and less error-prone method of formatting strings. F-Strings are formatted string literals that allows variables, functions and expressions to be embedded in a string. Following is the basic format of a python f-string.

variable = "World" print(f"Hello {variable}, This is a f-String")

The Complete Guide to Python f-Strings (1)

Before the introduction of f-Strings, the main methods of string formatting were percentage formatting (%) and the str.format() method. Let's look into each of these methods.

Percentage Formatting (%)

This is the original method of string formatting which was available in the python language from the inception. In this method, we use “%s” as a placeholder in a string. Then add the variables at the end of the string with a percentage sign (%). You have to use brackets for for multi variables. See the below example.

Single Variable

name = "Barry" string = "My name is %s." % name print(string)

The Complete Guide to Python f-Strings (2)

Multi-Variable

name = "Barry" age = 18 string = "My name is %s. I am %s years old." % (name, age) print(string)

The Complete Guide to Python f-Strings (3)

The main disadvantage of this type of formatting is that it gets complicated as he number of variables increase. It can make the code less readable and easily prone to errors, such as iterables like dictionaries and tuples not displaying correctly. The official Python 3 docs do not recommend this type of formatting, providing the following warning.

“The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly).

Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.”

str.format()

This style of formatting removes the usage of the percentage (%s) operator. This makes the syntax simpler and more readable. Formatting is now handled by .format() on a string object.

In str.format() method we can use named indexed {name}, numbered indexes {0} or implicit indexes as place holders in a string. Then call the .format() with the variables inside the brackets. See the below examples

name_value = "Barry" age_value = 18 namedindex_string = "My Name is {name}, I am {age} years old.".format(name = name_value, age = age_value) numberedindex_string = "My Name is {0}, I am {1} years old.".format(name_value, age_value) implicitindex_string = "My Name is {}, I am {} years old.".format(name_value, age_value) # Printing the formatted strings print(namedindex_string) print(numberedindex_string) print(implicitindex_string)

The Complete Guide to Python f-Strings (4)

You can see that str.format() is much more simple and functional method than percentage formatting (%). However, this method is still not suited for handling large sets of variables, as the code structure can get verbose quite easily. To mitigate these issues, and as a powerful method of formatting, python f-Strings were introduced.

f-Strings are also known as “formatted string literals” is a way in which we can use embedded python expressions inside string constraints. Formatted string literals are declared using “f” at the beginning with curly braces containing expressions that will be replaced with their values. f-Strings can be declared with either a lowercase “f” or an uppercase “F”. These expressions are evaluated at the runtime and formatted using the format protocol.

name = "Barry" # Lowercase f-String declaration string_one = f"Hello World, I am {name}" # Uppercase f-String declaration string_two = F"Hello World, I am {name}" print(string_one) print(string_two)

The Complete Guide to Python f-Strings (5)

As you can see f-Strings will make the code more readable while providing powerful functionality. f-String can be used with inline arithmetic operators, iterables, objects, functions etc..

As f-Strings are evaluated at runtime, we can use any valid python arbitrary expressions with it. f-Strings can run these expressions faster than any previous method of formatting. This is possible because expressions are evaluated at runtime and all the expressions inside the curly braces are evaluated in their scope then combined with the string literal of the f-String. Following sections will demonstrate how expressions are processed.

Arithmetic Operators

f-Strings with variables

price = 10 no_of_apples = 5 string = f"Price of {no_of_apples} apples is {int(price * no_of_apples)}" print(string)

The Complete Guide to Python f-Strings (6)

Inline arithmetic operations

string = f"Price of 5 apples is {5 * 10}" print(string)

The Complete Guide to Python f-Strings (7)

Functions in f-Strings

# Convert Given String to Hexadecimal def convert_to_hex(value): value_binary = value.encode(encoding='utf_8') hex_value = value_binary.hex() return hex_value name = "Barry" string = f"Hexadecimal value of Barry string is {convert_to_hex(name)}" print(string) # Convert Given Hexadecimal to String def convert_to_string(value): hex_value = bytes.fromhex(value) string_value = hex_value.decode(encoding='utf_8') return string_value hex_value = "4261727279" string = f"String value of 4261727279 hexadecimal is {convert_to_string(hex_value)}" print(string)

The Complete Guide to Python f-Strings (8)

In the above example, we call the functions “converttohex” and “converttostring” from the f-string to convert the given values to hex and then back to string format.

Calling Objects created from Classes

class PersonalDetails: def find_gender(self): if self.gender == "M": pronoun = "he" elif self.gender == "F": pronoun = "she" return pronoun def __init__(self, first_name, last_name, age, gender): self.first_name = first_name self.last_name = last_name self.age = age self.gender = gender def __str__(self): return f"{self.first_name} {self.last_name} is {self.age} years old." def __repr__(self): return f"{self.first_name} {self.last_name} is eligible for a driving licence as {self.find_gender()} is {self.age}" new_licence = PersonalDetails("Barry", "Stevens", "18","M") # Get String Representation print(f"{new_licence}") # Get Object Representation print(f"{new_licence!r}")

RESULTThe Complete Guide to Python f-Strings (9)

Using the str() and repr() methods, we make the Class “PersonalDetails”. A new instance of the class is created called new_licence and is used in f-Strings to call each function within the class. The str() function is the informal string representation of the object, while repr() function is the official representation of the object. By default, f-Strings identifies the str() function, because of this we have to explicitly call the repr() using the shorthand ‘!r’

Multiline f-Strings

When defining multi-line f-Strings, each line must start with the f otherwise it will not be recognized as an f-String and will not have the necessary formatting to obtain a single line or multi-line output.

Multiline f-String with a single line output

# Single Line Outputname = "Barry" email = "barry@gmail.com" addr = "56, Beach Road, Seattle" string = ( f"My name is {name}. " f"My email is {email}. " f"I live in {addr}." ) print(string)

The Complete Guide to Python f-Strings (10)

Multiline f-String with a multiline output

# Multi Line Output name = "Barry" email = "barry@gmail.com" addr = "56, Beach Road, Seattle" string = ( f"My name is {name}. \n" f"My email is {email}. \n" f"I live in {addr}." ) print(string)

The Complete Guide to Python f-Strings (11)

In this section, we will learn how to use special characters within f-Strings.

Quotation Marks

Any type of quotation mark can be used when defining an f-String. The only limitation is that you need to add a different quotation mark inside the string opposed to what is defined outside. Otherwise, this will cause a Syntax error.

f"{'Barry'}"

RESULTThe Complete Guide to Python f-Strings (12)

f"{"Barry"}"

The Complete Guide to Python f-Strings (13)

To use the same quotation mark in all instances of the string, we can use “\” as an escape character

name = "Barry" #Single quotations print(f"Hello, \'{name}\'") # Double quotations print(f"Hello, \"{name}\"")

The Complete Guide to Python f-Strings (14)

Other types of quotation marks

# Single Quote f'{"Hello, Barry"}' f"{'Hello, Barry'}" # Triple Quotes f"""Hello, Barry""" f'''Hello, Barry'''

The Complete Guide to Python f-Strings (15)

Braces

To use braces in an f-string, simply add a double set of braces. If triple braces are used it will result in only displaying a single set of braces. To overcome this limitation we can add additional braces so f-String will recognize each additional set of braces. Please refer to the following examples.

value = 50 # Double Braces print(f"The value of the variable is {{50}}") # Triple Braces print(f"The value of the variable is {{{50}}}") # Multiple Braces print(f"The value of the variable is {{{{50}}}}")

The Complete Guide to Python f-Strings (16)

Backslashes

Backslashes can be used in the string part of the f-String. However, backslashes can not be used inside the expression. This will result in a Syntax error.

Correct Use of backslashes

# Using Backslashes file_name = "info.txt" string = f"File Location is C:\Data\{file_name}" print(string)

The Complete Guide to Python f-Strings (17)

Incorrect use of backslashes

# Using Backslashes file_name = "info.txt" string = f"File Location is C:\Data{\file_name}" print(string)

RESULTThe Complete Guide to Python f-Strings (18)

You can see, as the backslash is used inside the expression, f-String can not identify the variable. This results in a Syntax error.

When dealing with comments, the same rules of backslashes are used. Comments or the hashtag (#) can be only added in the string portion of an f-String. If they are added to the expression of an f-String, this will also result in a Syntax error. Below examples will demonstrate adding comments in f-Strings.

Correct Use of Comments (#)

# Using Comments (#) name = "Barry" user_id = 7851 string = f"User {name}'s user ID is #{user_id}" print(string)

RESULTThe Complete Guide to Python f-Strings (19)

Incorrect Use of Comments(#)

# Using Comments (#) name = "Barry" user_id = 7851 string = f"User {name}'s user ID is {#user_id}" print(string)

RESULTThe Complete Guide to Python f-Strings (20)

Dictionaries

When referencing dictionaries in an f-String, the quotation marks referring to each key-value must be different from the f-String quotation mark. If we create an f-String with a double quotation (“”) marks, we must use the single quotation marks (‘’) to reference the key-value pair. Otherwise, this will also result in a Syntax error.

Referencing Dictionaries

# Dictionary user_details = {'first_name' : 'Barry', 'last_name' : 'Stevens', 'age' : '18', 'email' : 'barry@gmail.com'} # Multiline f-String string = ( f"== User Details == \n" f"Full Name : {user_details['first_name']} {user_details['last_name']} \n" f"Age : {user_details['age']} \n" f"Email : {user_details['email']}" ) print(string)

RESULTThe Complete Guide to Python f-Strings (21)

In the above example, we identify each key in the dictionary with a single quotation mark and create a multiline f-String composting of all the user details. In the next example, we can identify the syntax error that will occur if we used the same quotation marks as the one used in the initial f-String declaration.

#Dictionary user_details = {'username' : 'barry005', 'active_days' : '35'} # Incorrect f-String reference to the dictionary string = f"User {user_details["username"]} was active for {user_details['active_days']} days." print(string)

RESULTThe Complete Guide to Python f-Strings (22)

The syntax error occurs when we are referencing the username key in the dictionary. As we are calling the username key using the same type of quotation mark (double quotes), the f-String identification breaks causing the error. To mitigate this problem, simply reference the username key by using single quotations marks as shown below.

#Dictionary user_details = {'username' : 'barry005', 'active_days' : '35'} # Incorrect f-String reference to the dictionary string = f"User {user_details['username']} was active for {user_details['active_days']} days." print(string)

RESULTThe Complete Guide to Python f-Strings (23)

Final Thoughts

f-Strings are a paradigm shift in how Python handles string formatting. It Allows users to create strings with expressions directly attached. While anyone can use percentage formatting (%) or str.format() depending on the situation, f-String offers numerous advantages over the those methods such as being more readable, faster handling of data, and more convenient ways to insert variables, functions, and objects to a string constraint.

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:

The Complete Guide to Python f-Strings (2024)

FAQs

The Complete Guide to Python f-Strings? ›

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

How to do an F-string in Python? ›

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

How are F-strings implemented in Python? ›

In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values. Some examples are: >>> import datetime >>> name = 'Fred' >>> age = 50 >>> anniversary = datetime.

Are F-strings good in Python? ›

Using f-strings, your code will not only be cleaner but also faster to write. With f-strings you are not only able to format strings but also print identifiers along with a value (a feature that was introduced in Python 3.8).

What does F stand for in a string Python? ›

Also called formatted string literals, f-strings are string literals that have an f before the opening quotation mark. They can include Python expressions enclosed in curly braces. Python will replace those expressions with their resulting values.

What does .2f do in Python? ›

So %. 2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

How do you escape quotes in Python F string? ›

Python f-string Escaping Characters

For this purpose, we make use of escape characters in f-string. To escape a curly bracket, we double the character. While a single quote is escaped using a backslash.

How to round a number in Python f-string? ›

Rounding Numbers With F-Strings

F-strings can also be used to round numbers to a specific precision, using the round() function. To round a number using f-strings, simply include the number inside the curly braces, followed by a colon and the number of decimal places to round to.

How are strings written in Python? ›

Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). String literals can be enclosed by either double or single quotes, although single quotes are more commonly used.

What is the F-string prefix? ›

F-strings, also known as formatted string literals, were introduced in Python 3.6. They provide a concise and readable way to embed expressions inside string literals, making string formatting more intuitive and efficient. The 'f' prefix denotes f-strings before the opening quotation mark of a string.

What can I use instead of F-string in Python? ›

Python has several tools for string interpolation that support many formatting features. In modern Python, you'll use f-strings or the .format() method most of the time. However, you'll see the modulo operator ( % ) being used in legacy code.

What is the point of an F-string? ›

F-string is a way to format strings in Python. It was introduced in Python 3.6 and aims to make it easier for users to add variables, comma separators, do padding with zeros and date format. F-string was introduced in Python 3.6 and provides a better way to format strings.

How to use backslash in Python f string? ›

The backslash is a special character, which is used as part of an “escape sequence” in a string. For example \n denotes the newline character, \t denotes a tab, \” denotes a double quote, etc. For example: print("This is a double quoted \"string\"")

When were f-strings added to Python? ›

Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498.

Can we use F string in dictionary Python? ›

Python 3.6+ has introduced f-strings support in which keys of a dictionary can be used to format a string. In this approach, you need to place the f prefix before the string and place the key of the dictionary inside the curly braces { }.

Can we use F string in input Python? ›

In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.

How do you make an F string have no new line in Python? ›

Using Backslashes

The backslashes tell Python to ignore the newline characters in the code and join the f-strings into one single line. Using backslashes, you can create multiline strings with f-strings without having to use triple quotes or escape characters. This can make your code more compact and efficient.

Can you put a function in an F string? ›

This shows that you can directly insert the value of a variable into an f string. The syntax is easier than all of the other formatting options, such as the format() method or a % string. That's not all. f strings can also support functions or any other expression, evaluated inside the string.

How to type string in Python? ›

Assigning a string in Python is done by enclosing the text within either single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """).

References

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5536

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.