Python Print Without Escape Characters

5/5 - (1 vote)

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!"

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!"
# 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!"
# 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:

Python repr() Function -- A Helpful Guide with Example

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!"
# 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())
# 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:

Understanding Generators In Python

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!