Python Forum

Full Version: Docs missing info?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I watched "Python Programming" by Derek Banas on YouTube, he mentions newlines, and I decide to play around with it a little
print('hello', \n, 'world)
being the simplest example. I run the code expecting
Output:
hello world
but instead get a popup:
Error:
Syntax Error unexpected character after line continuation character
I figure whatever, not really important, and fiddle around until I get it working the same as the video. But then, I try another neat trick to only check the first letter of a word, which is supposed to be \b, and I'm not figuring out how to get it to work. No matter where I put it, I get the syntax error.
So I think, "Let's check the doc." Search for \b: no results. Search for \n: no results. Search for line continuation character: no results. Check index: no results. OK, let's try a different version. Change from 3.6 to 2: same thing.
Alright,
 if first != success
    try = again
So I spend 20 minutes looking on the forums for anything helpful and,
try = again
.
SO, does anyone know if I should be able to find this information in the docs? What is a line continuation character? Where should I look to find all the things I can use a backslash for?
you should pay more attention to details - you are missing number of quotes in the code

print('hello', '\n', 'world') # here \n is an escape sequence for new line
you can find more info here https://docs.python.org/3.6/reference/le...?#literals


and here is example from PEP8 for use of \ as line continuation char

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())
Fixed my typo to have the correct number of quotes, and it worked. I remember putting \n and \b in quotes, and getting the error, but I have alot of times where I have missed things I thought I hadn't. Thanks for the reference to literals and the PEP. Gonna be reading for awhile.
you can also put the escape characters in your string directly. Print will know how to handle.

print('hello\nworld') # again \n is an escape sequence for new line