Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raw string
#1
you are writing code (probably a function that can be used in a purpose-designed code generator program) that takes in data from a file and outputs code that is a Python string literal, with the proper quotes, that could be used anywhere a string literal is allowed to produce the entire contents of that file. your goals include readability for humans (like using \r instead of \015 for carriage return) and keeping both the whole result and the part between the quotes (whichever quotes are used) as short as possible (like using \r instead of \015 for carriage return) and and use single quotes unless something else meets the other goals better. how would your code output a file containing just one backslash character? would it output r'\' or '\\' ?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
The answer is '\\' because r'\' is a syntax error. This is what the repr() function outputs.
Reply
#3
A raw string cannot end in a single or odd \'s.

Quote:Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.

Ref: https://docs.python.org/3/reference/lexi...s-literals at the end of the subsection.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
if there is ever a "what would you like to see in Python4" one of my suggestions will be to make raw strings work even if it ends in an odd number of backslashes. this can be done. the string just needs to be treated as raw in the first pass of parsing (ignore any special semantics of \ and just look for the correct closing quote in that first pass). imagine in an alternate universe everything was the same except they use ! like we use \ where r'\' would thus be valid and r'!' would be an error.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Jan-11-2018, 04:58 AM)Skaperen Wrote: ignore any special semantics of \ and just look for the correct closing quote in that first pass
How do you find the correct closing quote if there is no way to escape quote characters? Think about r'\'\'\'\'
Reply
#6
the first matching quote closes a string literal of this type. maybe it should begin with q instead of r so both methods can be supported.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020