Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Docs missing info?
#1
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?
Reply
#2
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())
Reply
#3
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.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  where is documentation for stat result attributes in PDF docs? Skaperen 5 998 May-18-2023, 07:24 PM
Last Post: Skaperen
  frozenset.copy() docs wrong Skaperen 8 3,565 Apr-08-2020, 12:56 AM
Last Post: Skaperen
  looking up docs for open file methods Skaperen 3 2,393 Nov-10-2019, 01:08 AM
Last Post: Skaperen
  Operator Precedence Direction in Docs blitz4 3 3,097 Mar-11-2018, 06:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020