Use repr:
a = "Hello\tWorld\nHello World"
print(repr(a))
# 'Hello\tWorld\nHello World'
Note you do not get \s
for a space. I hope that was a typo…?
But if you really do want \s
for spaces, you could do this:
print(repr(a).replace(' ',r'\s'))
More Related Contents:
- Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
- How to print instances of a class using print()?
- What is the purpose of the return statement? How is it different from printing?
- How do I expand the output display to see more columns of a Pandas DataFrame?
- Print in one line dynamically
- How to print a single backslash?
- Process escape sequences in a string in Python
- How to use a variable inside a regular expression?
- How do I keep Python print from adding newlines or spaces? [duplicate]
- How can I remove the ANSI escape sequences from a string in python
- What is the difference between a string and a byte string?
- Python: TypeError: cannot concatenate ‘str’ and ‘int’ objects [duplicate]
- How to un-escape a backslash-escaped string?
- Why does the print function return None?
- How can I selectively escape percent (%) in Python strings?
- Print multiple arguments in Python
- How to print to stderr in Python?
- How to print bold text in Python?
- Print to standard printer from Python?
- How to block calls to print?
- Printing without newline (print ‘a’,) prints a space, how to remove?
- Child processes created with python multiprocessing module won’t print
- Python 3 print without parenthesis
- How can I redirect print output of a function in python [duplicate]
- Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
- How can I print bold text in Python?
- In Python, is it possible to escape newline characters when printing a string?
- How can I read a single character at a time from a file in Python?
- Printing a string prints ‘u’ before the string in Python?
- How can i parse a comma delimited string into a list (caveat)?