How To Print An Odd Number Of Backslashes In Python
I need to print a string as part of a list that has 3 backslashes in it in Python. However, this is not appearing to be as simple as I expected. print ['\\\'] Traceback (most rece
Solution 1:
'\\\\\\' is a string contain 3 backslashes. You can see that there are 3 characters in the string by applying list to it:
In[166]: list('\\\\\\')
Out[166]: ['\\', '\\', '\\']'\\'*3 would also work:
In[167]: list('\\'*3)
Out[167]: ['\\', '\\', '\\']Or since
In [169]: hex(ord('\\'))
Out[169]: '0x5c'you could avoid the need to escape the backslash by using \x5c:
In [170]: print('\x5c\x5c\x5c')
\\\
Solution 2:
printr"\\\ "would work I think (r indicates a literal string)
(as pointed out in the comments you cant end with a backslash in raw strings ...(so I added a space))
If you didnt want the space you could
printr"\\\ ".strip()
Post a Comment for "How To Print An Odd Number Of Backslashes In Python"