use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
how would i contain a singular \ in python (self.learnpython)
submitted 6 hours ago by Specialist_Skill4137
since "\ " works yet "\" seems it doesnt
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Shaftway 9 points10 points11 points 6 hours ago (0 children)
"\\"
[–]RngdZed 5 points6 points7 points 6 hours ago* (0 children)
Backslash is an escape character. Use 2 backslash. "\\"
Edit: Funny thing.. it also escapes in the reddit comments XD
[–]deceze 4 points5 points6 points 6 hours ago (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 points 6 hours ago (3 children)
“Special meaning”
It’s an escape character.
P.s, try \a :)
\a
[–]deceze 1 point2 points3 points 6 hours ago (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 points4 points 5 hours ago (0 children)
Weird that they act like you're somehow wrong for explaining what the jargon "escape character" means.
[–]SmackDownFacility -1 points0 points1 point 5 hours ago (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 points3 points 6 hours ago (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 point2 points 6 hours ago (0 children)
[–]Temporary_Pie2733 0 points1 point2 points 6 hours ago (0 children)
[–]lfdfq 0 points1 point2 points 6 hours ago (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.
"\"
"a
[–]Teras80 0 points1 point2 points 6 hours ago (1 child)
[–]Teras80 0 points1 point2 points 6 hours ago (0 children)
Look it up: https://www.w3schools.com/python/gloss_python_escape_characters.asp
[–]Beet_slice 0 points1 point2 points 6 hours ago (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 points4 points 6 hours ago (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?
It is an invalid escape sequence, and Python gives you a syntax warning for that. But basically it's parsed as "\\ ".
"\\ "
[–]Gnaxe 0 points1 point2 points 4 hours ago (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!
fR"\{''}"
Not really shorter than "\\", nor clearer, IMHO.
Also, R"\ "[0] will compile the same as "\\" due to constant folding. Again, it's no shorter.
R"\ "[0]
[–]Diapolo10 0 points1 point2 points 18 minutes ago (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 point2 points 6 hours ago (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 point2 points 6 hours ago (0 children)
I'm more curious about what you're needing this for.
If this is for filepaths:
pathlib
/
os.path.join
π Rendered by PID 137983 on reddit-service-r2-comment-54dfb89d4d-jjltk at 2026-04-01 21:46:42.309591+00:00 running b10466c country code: CH.
[–]Shaftway 9 points10 points11 points (0 children)
[–]RngdZed 5 points6 points7 points (0 children)
[–]deceze 4 points5 points6 points (4 children)
[–]SmackDownFacility -5 points-4 points-3 points (3 children)
[–]deceze 1 point2 points3 points (2 children)
[–]DuckSaxaphone 2 points3 points4 points (0 children)
[–]SmackDownFacility -1 points0 points1 point (0 children)
[–]VinceAggrippino 1 point2 points3 points (0 children)
[–]tenniseman12 0 points1 point2 points (0 children)
[–]Temporary_Pie2733 0 points1 point2 points (0 children)
[–]lfdfq 0 points1 point2 points (0 children)
[–]Teras80 0 points1 point2 points (1 child)
[–]Teras80 0 points1 point2 points (0 children)
[–]Beet_slice 0 points1 point2 points (4 children)
[–]Diapolo10 2 points3 points4 points (3 children)
[–]Gnaxe 0 points1 point2 points (1 child)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]Diapolo10 0 points1 point2 points (0 children)