Print unlimited space in the shell

Consider this line of Python code:

s = "This string has \n\r whitespace" 

How do i do

print s

give me

This string has \n\r whitespace

instead

 This string has whitespace 

as now.

+4
source share
4 answers

Do you want a raw string?

 s = r"This string has \n\r whitespace" 

or convert special characters to this representation?

 repr(s) 
+13
source
  print s.encode('string-escape') 
+7
source

You need the repr function.

 print repr(s) 
+2
source

You can use the python formatting capabilities to print a string in raw form:

 print "%r" % s 

You can also create a string in raw form, for example:

 s = r'This string has \n\r whitespace' 

and Python will handle the escape backslash, so this is exactly what you get:

 print s # outputs "This string has \n\r whitespace" 
+1
source

All Articles