Print a string with the special characters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Print a string with the special characters in Python #

Use the repr() function to print a string with the special characters, e.g. print(repr(my_str)). The repr() function returns a string containing a printable representation of the provided object.

main.py
# ✅ print string with special characters (repr()) my_str = 'first\nsecond\nthird\n' print(repr(my_str)) # 👉️ 'first\nsecond\nthird\n' # ---------------------------------- # ✅ print string with special characters (raw string) my_str = r'first\nsecond\nthird\n' print(my_str) # 👉️ first\nsecond\nthird\n # ---------------------------------- # ✅ print string with special characters (f-string) variable = 'second' my_str = fr'first\n{variable}\nthird\n' print(my_str) # 👉️ first\nsecond\nthird\n

We used the repr() function to print a string with the special characters.

main.py
my_str = 'first\nsecond\nthird\n' print(repr(my_str)) # 👉️ 'first\nsecond\nthird\n'
The repr() function returns a printable representation of the provided object rather than the string itself.

Alternatively, you can use the str.encode() and bytes.decode() methods to convert the string to a raw string before printing.

main.py
my_str = 'first\nsecond\nthird\n' result = my_str.encode('unicode_escape') print(result) # 👉️ b'first\\nsecond\\nthird\\n' result = my_str.encode('unicode_escape').decode() print(result) # 👉️ first\nsecond\nthird\n

The str.encode method returns an encoded version of the string as a bytes object.

We used the unicode_escape encoding to escape the special characters with an extra backslash.

The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8.

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

If you have access to the variable's declaration, you can prefix the string with r to mark it as a raw string.

main.py
my_str = r'first\nsecond\nthird\n' print(my_str) # 👉️ first\nsecond\nthird\n
Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

If you need to use variables in the raw string, use a formatted string literal.

main.py
variable = 'second' my_str = fr'first\n{variable}\nthird\n' print(my_str) # 👉️ first\nsecond\nthird\n

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

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

Notice that we prefixed the string with fr and not just with f or r.

The start of an expression in an f-string is marked using curly braces.

If you need to include the literal characters in the string, use two sets of curly braces.

main.py
variable = 'second' my_str = fr'{{->}}first\n{variable}\nthird\n' print(my_str) # 👉️ {->}first\nsecond\nthird\n
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.