Python has special “escape characters” that start with the single backslash such as
\". If you print a string with escape characters, Python prints the special meaning—for example, a new line for the newline character
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
The following example shows how the print output was unaffected by the escape characters—Python prints without the special meaning of the escape characters
s = "Learn\tprogramming\nwith\t\tFinxter!" print(repr(s)) # Learn\tprogramming\nwith\t\tFinxter!
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
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:
''.join()method to glue together all characters in an iterable. Learn more about this method in our detailed blog tutorial.
- Generator expression
<... for ... in ... if ...>to dynamically create an iterable of characters and filter out each non-alphanumeric character. Learn more about this method in our full blog guide.
string.isalnum()method to check for a given character whether it’s a non-special character. Learn more here.
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!
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.
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.