Alan Gauld <alan.ga...@btinternet.com> Wrote in message: > On 14/01/14 07:42, Christian Alexander wrote: > >> Why does the interactive prompt not recognize escape sequences in >> strings? It only works correctly if I use the print function in python 3. >> >> >>> "Hello\nWorld" >> "Hello\nWorld" > > That depends on how you define "correctly"... > > When you evaluate an expression at the Python prompt Python prints the > repr() of the value. For strings that includes the quotes and the \n > characters and any other special characters it finds. The print > function on the other hand prints the str() of the value and that > interprets the quotes etc out > > In general repr() is more useful for debugging since it shows any > 'hidden' whitespace characters. repr() by convention should return a > value that can be evaluated and assigned to a variable, although it > doesn't always do that for miore complex types. >
I'd like to elaborate, Christian. The escape sequence in the literal string has been processed by the interpreter in either case by the time the string object has been created. What makes the repr logic important is that it adds the quotation marks, and turns unprintables into escape sequences. It has absolutely no access to the literal you started with. See for example """this one""" or "\x41" -- DaveA ----Android NewsGroup Reader---- http://www.piaohong.tk/newsgroup _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor