Last updated: Apr 9, 2024
Reading timeยท4 min
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.
my_str = 'bobby\nhadz\ncom\n' print(repr(my_str)) # ๐๏ธ 'bobby\nhadz\ncom\n'
We used the repr()
function to print a string with the special characters.
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.
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
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
.
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.
my_str = r'bobby\nhadz\ncom\n' print(my_str) # ๐๏ธ bobby\nhadz\ncom\n
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.
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.
variable = 'hadz' my_str = fr'{{->}}bobby\n{variable}\ncom\n' print(my_str) # ๐๏ธ {->}bobby\nhadz\ncom\n
If you need to print a backslash:
print()
function to print the result.my_str = 'Bobby\\Hadz\\Com' print(my_str) # ๐๏ธ Bobby\Hadz\Com
The example uses a second backslash to escape each backslash character in the string.
\
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.
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.
my_str = 'Bobby\\\\Hadz\\\\Com' print(my_str) # ๐๏ธ Bobby\\Hadz\\Com
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.
my_str = r'Bobby\Hadz\Com' print(my_str) # ๐๏ธ Bobby\Hadz\Com my_str = r'Bobby\\Hadz\\Com' print(my_str) # ๐๏ธ Bobby\\Hadz\\Com
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.
variable = 'Hadz' my_str = fr'Bobby\{variable}\Com' print(my_str) # ๐๏ธ Bobby\Hadz\Com
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.
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:
r
to mark it as a raw stringYou can learn more about the related topics by checking out the following tutorials: