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"
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:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 3 years ago.
I know that in Python you can use the array selector to retrieve a certain part of a string, ie me.name[10:] to get just the last 10 characters.
but how would you retrieve just the part of a string after an underscore ie _ using a single expression?
For example if my string is "stringcharThatChange_myname"
How would I extract just 'myname' ? I'm confined to using Python 3.5.1
You could use split.
test_string = "stringcharThatChange_myname"
print(test_string.split('_')[1]) # myname
Using split method.
"this will be excluded_this is kept".split('_')[1]
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)
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'