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\\")
Related
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("\")
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Remove specific characters from a string in Python
(26 answers)
Closed 2 years ago.
s = "Bob hit a ball!, the hit BALL flew far after it was hit."
I need to get rid of the following characters from s
!?',;.
How to achieve this with re.sub?
re.sub(r"!|\?|'|,|;|."," ",s) #doesn't work. And replaces all characters with space
Can someone tell me what's wrong with this?
The problem is that . matches all characters, not the literal '.'. You want to escape that also, \..
But a better way would be to not use the OR operator |, but simply use a character group instead:
re.sub(r"[!?',;.]", ' ', s)
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.