Python Print Without Escape Characters

Problem Formulation

Python has special “escape characters” that start with the single backslash such as \n, \t, and \". If you print a string with escape characters, Python prints the special meaning—for example, a new line for the newline character \n.

s = "Learn\tprogramming\nwith\t\tFinxter!"
print(s)

You can see that the output prints the special meaning of the escape characters:

Learn	programming
with		Finxter!

How to print the string in its original form without interpreting the escape characters?

This is what we want:

s = "Learn\tprogramming\nwith\t\tFinxter!"
print(s)
# Learn\tprogramming\nwith\t\tFinxter!

Solution 1: repr() – Print Without Interpreting Escape Characters

If you do want to print the escape characters in a string without interpreting them, i.e., skip their special meaning, use the built-in repr(s) function on the string s.

The following example shows how the print output was unaffected by the escape characters—Python prints without the special meaning of the escape characters \t and \n.

s = "Learn\tprogramming\nwith\t\tFinxter!"
print(repr(s))
# Learn\tprogramming\nwith\t\tFinxter!

Python’s built-in repr(obj) function returns the standard string representation of the provided object. The function internally calls the method obj.__repr__() which is defined per default for all objects.

You can learn more about the function in my detailed blog article and the following video tutorial:

Solution 2: Print Raw String to Ignore Special Meaning of Escape Chars

Alternatively, you can print a raw string r"..." to print the string without interpreting the escape characters. For example, the statement print('r"Learn\tprogramming\nwith\t') will simply print the raw string "Learn\tprogramming\nwith\t".

s = r"Learn\tprogramming\nwith\t\tFinxter!"
print(s)
# Learn\tprogramming\nwith\t\tFinxter

This solution is actually the same as the first one because Python internally calls the repr() function on the raw string.

However, what if you actually want to remove all escape characters from the string output?

Solution 3: Filtering Out All Escape Characters with string.isalnum()

To clean a string from escape and all other special characters, you can filter out all characters that are not alphanumeric using the string.isalnum() method like so: ''.join(c for c in s if c.isalnum()). This returns a new string that is free from all escape characters.

Let’s have a look at this code snippet that removes the newline and tabular escape characters from the string:

s = 'Learn\tprogramming\nwith\t\tFinxter!'
s_clean = ''.join(c for c in s if c.isalnum())
print(s_clean)
# LearnprogrammingwithFinxter

This code makes use of three concepts:

I suggest you watch the following introduction on generator expression if you don’t feel very confident yet:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!