I would like to display the escape characters when using print statement. E.g.
a = "Hello\tWorld\nHello World" print a Hello World Hello World
I would like it to display: "Hello\tWorld\nHello\sWorld"
Using 'str. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
To display them, Java has created a special code that can be put into a string: \". Whenever this code is encountered in a string, it is replaced with a double quotation mark.
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With