Hi,
While providing file path in Python, I use code in below manner
wbkName = r’\AA\AA\AA\AA\Python Chart Data.xlsx’
If I do not give r’ in my code it gives me bug. I have copied this line from somewhere and edited to suit my requirement.
Can anyone please explain me what does r’ stands for.
Mholscher
(Menno Hölscher)
March 5, 2020, 11:55am
2
The documentation does a good job:
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
If you see a quoted string preceded by a letter, this is a string that has different properties. An ‘r’ preceding a string denotes a raw, (almost) un-escaped string. The escape character is backslash, that is why a normal string will not work as a Windows path string.
1 Like
encukou
(Petr Viktorin)
March 5, 2020, 11:56am
3
1 Like
The r prefix on strings stands for “raw strings”.
Standard strings use backslash for escape characters:
“\n” is a newline, not backslash-n
“\t” is a tab, not backslash-t
“\u03C0” is the Greek letter pi, π, not backslash-u-zero-three-c-zero
and so forth. If you want to use backslashes in Windows pathnames, you
need to escape them by doubling:
wbkName = '\\AA\\chart.xlsx'
Alternatively, you can use raw strings, which switches off almost all
of the escape character processing:
wbkName = r'\AA\chart.xlsx'
or finally, and this is generally the recommended way, you can just use
forward slashes, since Windows will accept them as well:
wbkName = '/AA/chart.xlsx'
1 Like
Hi @Mholscher , thanks a lot for the help. Have a nice day ahead.
Hi @encukou , thanks a lot for the help. Have a nice day ahead.
Hi @steven.daprano , thanks a lot for the help. Have a nice day ahead.