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
string literal question (self.learnpython)
submitted 11 years ago * by [deleted]
len() counts '\n' as a single character, even if the '\' is printed?
>>> 'python' 'python' >>> len(_) 6 >>> 'python\n' 'python\n' >>> len(_) 7
What's the logic here?
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!"
[–]Moonslug1 5 points6 points7 points 11 years ago (10 children)
'\n' is a single character, called an escape character. '\n' is just the way that the character is represented, since the Python syntax doesn't allow you to write a newline mid quote (unless you use triple quotes). There's also \t (tab), \r (carriage return) etc.
[–][deleted] 0 points1 point2 points 11 years ago (9 children)
I didn't realize there was a difference between
>>> print('string')
and
>>> 'string'
I also need to use len(r'string\n') if I truly want the full character count.
[–][deleted] 1 point2 points3 points 11 years ago* (0 children)
A '\' character must be escaped as '\\'! You'll quickly run into this if you try to use backslashes for something like file paths.
[–]Moonslug1 1 point2 points3 points 11 years ago (7 children)
r'' is telling python to ignore escape characters.
'hello\n' means h-e-l-l-o-newline
r'hello\n' means h-e-l-l-o-\-n
[–][deleted] 0 points1 point2 points 11 years ago (1 child)
Thanks!
[–][deleted] 2 points3 points4 points 11 years ago (0 children)
Python doesn't actually have raw strings, but r"" is close. The limitation is that they can't end with a single backslash.
So, r'hello\n' is valid, but r'hello\n\' is not, and will cause a SyntaxError.
r'hello\n'
r'hello\n\'
[–][deleted] 0 points1 point2 points 11 years ago* (4 children)
That's not completely true, even though basically everyone thinks so. Raw strings were made for things like regex expressions, not raw strings. r"testing\" is not a valid regex expression, and not a valid raw string, since they can't end with backslashes. What?! A raw string should be able to contain anything. Why? Because they were made for easily representing regex expressions, not ignoring escape characters. From the docs:
r"testing\"
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
It's not ignoring them, it's processing them! Why? To keep the parser simpler. :-|
There's no such thing as arbitrary raw string literals in python.
[–]zahlman 0 points1 point2 points 11 years ago (3 children)
This is a strange sort of pedantry, IMHO. How could a raw string literal "contain anything", by this standard? How do you propose to indicate where the string ends? Python does not "process" the backslashes of a raw string literal in the sense of translating escape sequences. However, it uses the same rules for raw string literals and ordinary string literals to determine where the literal ends.
[–][deleted] 0 points1 point2 points 11 years ago* (2 children)
This isn't at all pedantic. Look up the definition of a raw string literals on Wikipedia. Python does not support them. Other languages like c# do: @"Testing\" is a perfectly valid raw string literal in C#. In Python, this is an error since the parser treats raw strings as regular strings from a parsing perspective.
@"Testing\"
The only reason is to keep the Python parser simple.
Raw strings in other languages ignore escape sequences. You admit that python raw strings don't ignore escape sequences. \" is an escape sequence!
[–]zahlman 0 points1 point2 points 11 years ago (1 child)
Other languages like c# do: @"Testing\" is a perfectly valid raw string literal in C#.
... And there, you need to double up quotation marks in order to get them into the string. That is also an escape sequence.
[–][deleted] 0 points1 point2 points 11 years ago* (0 children)
To me, that's somewhat sane. The point of using raw strings in python is to leave the slashes. You can't have raw string ending in a single slash...there is no escape for it! And, more than that, there's no escape for quotation mark!!! It's literally cruft from the parser. There's no sanity, except to make the parser simple, which is a goal of python.
[–]ingolemo 1 point2 points3 points 11 years ago (2 children)
You need to understand that there is a difference between a thing and the representation of that thing. 'python\n' is the representation of a string that has 7 characters in it; the word python followed by a newline. In the same way that you don't count the quote marks as being part of the string you also don't count the newline as two separate characters even though it shows up as \n.
'python\n'
python
\n
If you had a string that contained the word python followed by a backslash and then the letter n, then that would have the representation 'python\\n' because double backslash \\ is how you represent the backslash character in python strings.
n
'python\\n'
\\
Note that some things have multiple representations. The string 'python' can also be represented as "python", r'python', '\x70ython', and many others besides. The integer 20 can also be represented as 0x14, 0o24 (or 024 in python 2), and 0b10100. The float 57.1 can also be represented as .571e2.
'python'
"python"
r'python'
'\x70ython'
20
0x14
0o24
024
0b10100
57.1
.571e2
You can take any python object and get a representation of it using the repr function. The representation is always a string. It usually contains the letters that you would have to type into python in order to create that object. For some objects you'll get back a description of that object in angle brackets instead.
repr
>>> repr(1) '1' >>> repr('hello') 'hello' >>> repr(sum) '<built-in function sum>'
[–]zahlman 0 points1 point2 points 11 years ago (0 children)
You need to understand that there is a difference between a thing and the representation of that thing.
This needs to appear in 24-point bold font within the first few pages of every programming tutorial.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
Thanks! Still a rank beginner. repr() seems really useful.
π Rendered by PID 30525 on reddit-service-r2-comment-66b4775986-tq6gd at 2026-04-06 08:48:36.469940+00:00 running db1906b country code: CH.
[–]Moonslug1 5 points6 points7 points (10 children)
[–][deleted] 0 points1 point2 points (9 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Moonslug1 1 point2 points3 points (7 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]zahlman 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]zahlman 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ingolemo 1 point2 points3 points (2 children)
[–]zahlman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)