Print a String with the Special Characters in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Print a string with the special characters in Python
  2. Print a string with the special characters using encode() and decode()
  3. Print a string with the special characters using a raw string
  4. Print a backslash in Python
  5. Print a backslash using a raw string

# 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
my_str = 'bobby\nhadz\ncom\n' print(repr(my_str)) # ๐Ÿ‘‰๏ธ 'bobby\nhadz\ncom\n'

print string with special characters

The code for this article is available on GitHub

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

The repr() function returns a printable representation of the provided object rather than the string itself.

# Print a string with the special characters using encode() and decode()

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 = 'bobby\nhadz\ncom\n' result = my_str.encode('unicode_escape') print(result) # ๐Ÿ‘‰๏ธ b'bobby\\nhadz\\ncom\\n' result = my_str.encode('unicode_escape').decode() print(result) # ๐Ÿ‘‰๏ธ bobby\nhadz\ncom\n

print string with special characters using encode and decode

The code for this article is available on GitHub

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.

# Print a string with the special characters using a raw 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'bobby\nhadz\ncom\n' print(my_str) # ๐Ÿ‘‰๏ธ bobby\nhadz\ncom\n

print string with special characters using raw string

The code for this article is available on GitHub
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 = 'hadz' my_str = fr'bobby\n{variable}\ncom\n' print(my_str) # ๐Ÿ‘‰๏ธ bobby\nhadz\ncom\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 = 'hadz' my_str = fr'{{->}}bobby\n{variable}\ncom\n' print(my_str) # ๐Ÿ‘‰๏ธ {->}bobby\nhadz\ncom\n

# Print a backslash in Python

If you need to print a backslash:

  1. Use a second backslash character to escape each backslash in the string.
  2. Use the print() function to print the result.
main.py
my_str = 'Bobby\\Hadz\\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\Hadz\Com

print backslash in python

The code for this article is available on GitHub

The example uses a second backslash to escape each backslash character in the string.

The backslash \ character has a special meaning in Python - it is used as an escape character (e.g. \n or \t).

By adding a second backslash, we treat the \ as a literal character.

main.py
my_str = 'Bobby\\Hadz\\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\Hadz\Com

If you need to print two backslash characters next to one another, use four backslashes.

main.py
my_str = 'Bobby\\\\Hadz\\\\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\\Hadz\\Com

# Print a backslash using a raw string

Alternatively, you can use a raw string.

When a string is prefixed with r, it treats backslashes as literal characters and escaping them is not necessary.

main.py
my_str = r'Bobby\Hadz\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\Hadz\Com my_str = r'Bobby\\Hadz\\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\\Hadz\\Com

print backslash using raw string

The code for this article is available on GitHub
Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

There is no need to escape backslash characters when using a raw string.

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

main.py
variable = 'Hadz' my_str = fr'Bobby\{variable}\Com' print(my_str) # ๐Ÿ‘‰๏ธ Bobby\Hadz\Com
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 the string is prefixed with fr and not just with f.

If you are constructing a path, e.g. to a directory or a file, you can use forward slashes instead of backslashes.

main.py
file_name = 'C:/Users/Bobby/Desktop/example.txt' # ๐Ÿ‘‡๏ธ C:/Users/Bobby/Desktop/example.txt print(file_name)

A forward slash can be used in place of a backslash when you need to specify a path.

Backslash characters have a special meaning in Python, so to treat them as literal characters, we have to:

  • escape each backslash with a second backslash
  • prefix the string with r to mark it as a raw string
  • use forward slashes in place of backslashes in a path

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev