Combine f-string and raw string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Combine f-string and raw string in Python #

Prefix the string with fr to combine an f-string and a raw string, e.g. my_str = fr'C:\Users\Alice\Desktop\{file_name}'. When a string is prefixed with fr it is marked as a raw, formatted string literal.

main.py
file_name = 'example.txt' # ✅ Combining an f-string with a raw string my_str = fr'C:\Users\Alice\Desktop\{file_name}' print(my_str) # 👉️ 'C:\Users\Alice\Desktop\example.txt' # --------------------------------------------------------- # 👇️ If you have to use literal curly braces, use two sets variable = 'second' my_str = fr'{{->}}first\t{variable}\nthird' print(my_str) # 👉️ '{->}first\tsecond\nthird'

We prefixed the string with fr to mark it as a raw, formatted string literal.

You can use curly braces to insert variables and expressions into the raw string.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals can be combined with a raw string by prefixing the string with fr.

main.py
file_name = 'example.txt' my_str = fr'C:\Users\Alice\Desktop\{file_name}' print(my_str) # 👉️ 'C:\Users\Alice\Desktop\example.txt'
Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

Expressions in f-strings are wrapped in curly braces, so if you have to use literal curly braces in the string, you have to use two sets.

main.py
variable = 'second' my_str = fr'{{->}}first\t{variable}\nthird' print(my_str) # 👉️ '{->}first\tsecond\nthird'

The first set of curly braces marks the start of an expression and the second set represents the literal characters.

You can also use the str.format() method in a similar way.

main.py
my_str = r'C:\Users\Alice\Desktop\{file_name}'.format( file_name='example.txt' ) print(my_str) # 👉️ C:\Users\Alice\Desktop\example.txt

Notice that we only prefixed the string with r to mark it as a raw string.

The str.format method performs string formatting operations.

main.py
first = 'James' last = 'Doe' result = "His name is {0} {1}".format(first, last) print(result) # 👉️ "His name is James Doe"

The string the method is called on can contain replacement fields specified using curly braces {}.

Each replacement field should contain either the numeric index of a positional argument or the name of a keyword argument.

main.py
first = 'James' last = 'Doe' result = "His name is {f} {l}".format(f=first, l=last) print(result) # 👉️ "His name is James Doe"

The example above uses keyword arguments instead of positional ones.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.