Why does print "\\" only print out a single backslash? [duplicate] - python

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.

Related

Why I cannot print the character "\" in Python [duplicate]

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("\")

(Python 2.7) Regex Replace with similar character in replacement string as in pattern [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 3 years ago.
import re
pattern = re.compile(r"/")
a = "a/b"
I tried
re.sub(pattern, '\/', a)
#(also, a.replace('/', '\/'))
#output
a\\/b
What I want is
a\/b
a.replace('/', '\\/')
the first \ is an escape character, so you need to type it twice to have the real \.
You can use if it's not compulsory to use regex:
a = "a/b"
a=a.replace("/","\/")
print(a)

How can i print '\' in python? [duplicate]

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('\\')

How to deal with strings containing character codes? [duplicate]

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'

Python typing "\" [duplicate]

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\\")

Categories

Resources