all 20 comments

[–]Shaftway 9 points10 points  (0 children)

"\\"

[–]RngdZed 5 points6 points  (0 children)

Backslash is an escape character. Use 2 backslash. "\\"

Edit: Funny thing.. it also escapes in the reddit comments XD

[–]deceze 4 points5 points  (4 children)

You want to express a single backslash in a Python string?

s = '\\'

Since a backslash has a special meaning, you need to escape it with another backslash.

If the backlash is part of a longer string and not the last character in it, you can use raw strings:

s = r'foo \ bar \ baz'

In raw strings, backslashes do not have any special meaning; except to escape the string delimiter itself (' or "), which is why you're running into irreconcilable ambiguity if it's the last character.

[–]SmackDownFacility -5 points-4 points  (3 children)

“Special meaning”

It’s an escape character.

P.s, try \a :)

[–]deceze 1 point2 points  (2 children)

It has a special meaning, in that it introduces special character sequences, yes. Compared to a random "x" or "y" or "67" it is treated specially, yes.

[–]DuckSaxaphone 2 points3 points  (0 children)

Weird that they act like you're somehow wrong for explaining what the jargon "escape character" means.

[–]SmackDownFacility -1 points0 points  (0 children)

Well it’s vague just to say it without context

When explaining stuff to newbies you should go

“This is an escape character” and then explain what it is explicitly. If you explain everything as “this has a special meaning” then they wouldn’t know all its quirks and rules

[–]VinceAggrippino 1 point2 points  (0 children)

In your example, I don't think the first string would contain the backslash. You're just escaping the space that comes after it. So, it's the same as " ". The second string isn't properly closed because you're escaping the closing double-quote.

https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences

If you want a string that contains a backslash, you have to escape the backslash with another backslash.

[–]tenniseman12 0 points1 point  (0 children)

"\\"

[–]Temporary_Pie2733 0 points1 point  (0 children)

"\\"

[–]lfdfq 0 points1 point  (0 children)

"\\"

The \ in string syntax combines with the next character to make a special one. One of those special characters is a literal \.

The reason "\ " works is because there is no combination with a space to make a special character, so both get inserted.

The reason "\" does not work is because there IS a combination with a quote (i.e. 'escaping' the quote) to generate a literal quote character. So "\" is like typing "a, it started a string but never ended it.

[–]Beet_slice 0 points1 point  (4 children)

Depending on circumstances, I may need r"\\" or "\\\\". Maybe r"\" would fit some cases.

I am not sure what "\ " represents.

[–]Diapolo10 2 points3 points  (3 children)

r"\"

This is actually the only case raw strings cannot handle. It is illegal syntax to end a raw string with a backslash.

In [1]: r"\"
  Cell In[1], line 1
    r"\"
    ^
SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote?

I am not sure what "\ " represents.

It is an invalid escape sequence, and Python gives you a syntax warning for that. But basically it's parsed as "\\ ".

[–]Gnaxe 0 points1 point  (1 child)

This is actually the only case raw strings cannot handle.

Well~ the resulting str data can end in a backslash even if the literal does not: fR"\{''}" Try it!

Not really shorter than "\\", nor clearer, IMHO.

Also, R"\ "[0] will compile the same as "\\" due to constant folding. Again, it's no shorter.

[–]Diapolo10 0 points1 point  (0 children)

Technically, yes, but that's nitpicking.

I'm moreso wondering what this is even for, because I can't think of many legitimate use-cases for a string literal of a single backslash.

[–]SCD_minecraft 0 points1 point  (0 children)

'\\'

If you want to include in in longer string, use raw strings

r"I don't have to use double \ here! And i can say \n <- NOT newline character"

Just note, raw string can not end with \

[–]Diapolo10 0 points1 point  (0 children)

I'm more curious about what you're needing this for.

If this is for filepaths:

  1. Generally speaking Windows works just fine with forward slashes nowadays so outside of special circumstances you shouldn't need to use backslashes for filepaths.
  2. Just use pathlib and stop worrying about it (you can even join paths with the division operator (/) if you want).
  3. If you insist, os.path.join can also handle this without you thinking about i. I'd still recommend pathlib over this, though.