Reputation: 17879
Consider this line of Python code:
s = "This string has \n\r whitespace"
How do I make
print s
give me
This string has \n\r whitespace
instead of
This string has
whitespace
as it does now.
Upvotes: 13
Views: 16099
Reputation: 4220
You can use python's formatting capabilities to print the string in its "raw" form:
print "%r" % s
You can also create a string in raw form like this:
s = r'This string has \n\r whitespace'
and Python will handle escaping the backslashes so that that is exactly what you get:
print s # outputs "This string has \n\r whitespace"
Upvotes: 1
Reputation: 31182
do you want a raw string ?
s = r"This string has \n\r whitespace"
or to transform special characters to it's representation?
repr(s)
Upvotes: 27