On 15/08/2019 12:17:36, Petr Viktorin wrote:
On 8/15/19 10:40 AM, Greg Ewing wrote:
If we want a truly raw string format that allows all characters, including any kind of quote, we could take a tip from Fortran:
s = 31HThis is a "totally raw" string!
Or from Rust:
let s = r"Here's a raw string"; let s = r#"Here's a raw string with "quotes" in it"#; let s = r##"Here's r#"the raw string syntax"# in raw string"##; let s = r###"and here's a '##"' as well"###; _______________________________________________ I rather like the idea! (Even though it would add to the proliferation of string types.) Obviously Python can't use # as the special character since that introduces a comment, and a lot of other possibilities are excluded because they would lead to ambiguous syntax. Say for the sake of argument we used "!" (exclamation mark). Possible variations include: (1) Like Rust: s = r"Here's a raw string"; s = r!"Here's a raw string with "quotes" in it"!; s = r!!"Here's r!"the raw string syntax"! in raw string"!!; s = r!!!"and here's a '!!"' as well"!!!; (2) Same, but omit the leading 'r' when using !: s = r"Here's a raw string"; s = !"Here's a raw string with "quotes" in it"!; s = !!"Here's a raw string with "quotes" and !exclamation marks! in it"!!; s = !!!"and here's a '!!"' as well"!!!; # Cons: Would conflict with adding ! as an operator (or at minimum, as a unary operator) for some other purpose in future. # Makes it less obvious that a !string! is a raw string. (3) Allow the user to specify his own delimiting character: s = r!|This raw string can't contain a "bar".| (4) As above, but the "!" is not required: s = r|This raw string can't contain a "bar".| # In this case the delimiter ought not to be a letter # (it might conflict with current or future string prefixes); # this could be forbidden. (5) Similar, but allow the user to specify his own delimiting *string* (specified between "!"s) (as long as it doesn't contain !): let s = r!?@!Could this string could contain almost anything? Yes!?@ # The text in this string is: # Could this string could contain almost anything? Yes! (6) Same except the first "!" is not required. In this case the first character of the delimiting string should not be a letter: let s = r?@!Could this string could contain almost anything? Yes!?@ # The text in this string is: # Could this string could contain almost anything? Yes!
I can dream ... A point about the current syntax: It is not true that a raw string can't end in a backslash, as https://en.wikipedia.org/wiki/String_literal points out. It can't end in an *odd number* of backslashes. 42 is fine, 43 is no good. Which makes it seem even more of a language wart (think of program-generated art). Rob Cliffe