This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 2 years ago.
I use print() function to use like this:print("\")and i get an exception. Tell me how to slove it. Thks
Use \\ instead.
Actually, \ is a special character and you have to escape it.
print("\\") # print a single "\" character
Use:
print("\\")
instead of print("\")
Related
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed last year.
I want to assing a path to a variable a:
a = "D:\misc\testsets\Real"
How can i omit the \t metacharacter without changing the folder name?
Use raw strings:
a = r"D:\misc\testsets\Real"
Try this:
r denotes raw string.
a = r"D:\misc\testsets\Real"
This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 4 years ago.
Is there any way to print back slash in python? we can write a string in three format.
1. ASCII
2. Unicode
3. Raw String
I have tried with all 3 formats but not able to get expected result.
Thanks in Advance
Use double backslash, first one marks the escape character:
print("\\")
First option - Unicode:
print('\u005c')
Second option:
print('\\')
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 6 years ago.
\201 is a character code recognised in Python. What is the best way to ignore this in strings?
s = '\2016'
s = s.replace('\\', '/')
print s #6
If you have a string literal with a backslash in it, you can escape the backslash:
s = '\\2016'
or you can use a "raw" string:
s = r'\2016'
This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
Consider the following Python 2.7 code:
print "\\"
Expected result: \\
Actual result: \
Why does Python only print out a single backslash?
It's because \ is the escape character, it escape sequences like newlines and carriage returns. To print out two you can do:
print "\\\\"
Or:
print r"\\"
r prefix tells to ignore escape characters.
This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
I want to print a string that ends with the \ character, but the problem is it makes the following ") part of the string, so won't work. Is there any way to end a string with \ being considered a regular character?
print("somestuffhere\") and this is still part of that string...
Put another "\" character behind it. This escapes the escape character. Like so:
print("somestuffhere\\")