This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 7 years ago.
Very simple question for Python 2:
I am calling specific library/function passing filename with readonly flag:
myfunction(r'/tmp/file.txt')
I wanted to replace it with variable:
filename = '/tmp/file.txt'
myfunction(r????)
How can I call that function?
That is not readonly flag. That means raw string. You use it when you don't want escape sequences inside string to be interpreted (like \n, \t etc.) See https://docs.python.org/2.0/ref/strings.html
For your string, you don't need it since it does not contain any escape sequence. Just omit the leading r.
filename = r'/tmp/file.txt'
myfunction(filename)
in most cases you can use
myfunction(r''+filename)
if you don't want to define the r prefix in your variable. This works with b'' and u'' too.
Related
This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!
This question already has an answer here:
Python string literal concatenation
(1 answer)
Closed 2 years ago.
print('Hello''World')
>>> HelloWorld
How come this works, when multiple string arguments are not separated by comma?
I found this unusual because, like any other function multiple parameters must be separated with commas.
There should have been a syntax error.
In Python, adjacent string literals are concatenated by default.
s = 'Hello' "world" '''!''' """?"""
This is perfectly fine. I used different quotes for each literal, and even separate them with spaces, and everything is still fine.
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 5 years ago.
In F# there is something called a literal string (not string literal), basically if a string literal is preceded by # then it is interpreted as-is, without any escapes.
For example if you want to write the path of a file in Windows(for an os.walk for example) you would do it like this:
"d:\\projects\\re\\p1\\v1\\pjName\\log\\"
Or you could do this(the F# way):
#"d:\projects\re\p1\v1\pjName\log\"
The second variant looks much more clear and pleasing to the eye. Is there something of the sort in python? The documentation doesn't seem to have anything regarding that.
I am working in Python 3.6.3.
There are: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
You can use r prefix.
https://docs.python.org/2.0/ref/strings.html
TL;DR use little r
myString = r'\n'
This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I have a config.ini file containing delimiter = \t which i now want to read using the python3 ConfigParser.
However, the resulting string is '\\t' instead of '\t' which breaks my program.
Is there a more elegant option to solve this problem instead of just manually stripping the extra '\' from every variable containing an escaped character?
I cannot find an option for ConfigParser().read() to not escape the backslash it finds in the file.
Python3 has a 'unicode_escape' codec.
r"a\tb".decode('unicode_escape')
'a\tb'
Sources:
https://bytes.com/topic/python/answers/37952-escape-chars-string
how do I .decode('string-escape') in Python3?
This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I receive a string like this from a third-party service:
>>> s
'\\u0e4f\\u032f\\u0361\\u0e4f'
I know that this string actually contains sequences of a single backslash, lowercase u etc. How can I convert the string such that the '\\u0e4f' is replaced by '\u0e4f' (i.e. '๏'), etc.? The result for this example input should be '๏̯͡๏'.
In 2.x:
>>> u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
u'\u0e4f\u032f\u0361\u0e4f'
>>> print u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
๏̯͡๏
There's an interesting list of encodings supported by .encode() and .decode() methods. Those magic ones in the second table include the unicode_escape.
Python3:
bytes("\\u0e4f\\u032f\\u0361\\u0e4f", "ascii").decode("unicode-escape")