Python Forum
Unexpected character after line continuation character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected character after line continuation character
#1
I have a simple assignment to basically get name and address information and display it neatly with a single print statement. I'm getting an error "Unexpected character after line continuation character", but Googling the issue has brought me here to ask the question and hopefully get an answer as it pertains to my code.

firstName = input("What is your first name? ")
lastName = input("What is your last name? ")
houseNumber = input("What is your house number? ")
streetName = input("What is the name of the street you live on? ")
cityName = input("What city do you live in? ")
stateName = input("What is the two letter abbreviation of the state you live in? ")
zipCode = input("What is your zip code? ")
print(firstName, lastName\nhouseNumber, streetName\ncityName, stateName  zipCode)
I know it's something simple that I'm just not seeing, all of the other simple programs I've done so far have had only minor issues that I was able to resolve. This, however, is bugging the crap out of me and I hope someone can show me where the mistake is.

Thanks in advance to anyone with any advice. This is the second week of class and I'm actually enjoying every bit of it! Working towards a B.S. in Electronics Engineering.
Reply
#2
You cannot have a raw \ character in python code, except at the end of a line to indicate a line continuation. Your last line of code should be
print(firstName, lastName, '\n',houseNumber, streetName,'\n', cityName, stateName,  zipCode)
but it would be better to write
print(firstName, lastName)
print(houseNumber, streetName)
print(cityName, stateName,  zipCode)
Reply
#3
Much Appreciated!

The stipulation for the assignment was to use a single print statement.

Your suggestion did clear up the issue though, my only question is why does the \n work when enclosed in quotes? Doesn't that denote a string type? I know a double backslash is required to print a backslash, so clearly I'm missing something...

My single print statement that I came up with after your advice is:

print(firstName, lastName,'\n' +houseNumber, streetName,'\n' +cityName +',' ' ' +stateName +'  '  +zipCode)
I feel like that is very clumsy but it outputs neatly as it should, like this:

First name Last Name
Street Address
City, State Zip (Two spaces between State and Zip)

I was getting syntax errors without the + before the variables following the \n, is there another way to accomplish this same output in a single print statement?

The professor specifically made note of the two spaces between state and zip. The focus of the other examples has been line breaks and a few other control codes.
Reply
#4
(Sep-05-2018, 06:07 AM)joshyb123 Wrote: my only question is why does the \n work when enclosed in quotes? Doesn't that denote a string type?
Exactly. A text between quotes is technically called a literal string. These strings may contain ordinary characters and some special characters that cannot be directly typed with a keyboard. They are entered with an escape sequence such as \n. When this escape sequence appears in a literal string, it is transformed in a single character named ASCII-linefeed.
(Sep-05-2018, 06:07 AM)joshyb123 Wrote: is there another way to accomplish this same output in a single print statement?
Yes there is, one can use the format() method
print('{} {}\n{} {}\n{}, {}  {}'.format(firstname, lastname, houseNumber, streetName, cityName, stateName, zipCode))
Reply
#5
Thank you very much, Gribouillis.

You've been most helpful. Not only did you point out my error, but you have provided a great explanation of relevant information.
Reply
#6
If using 3.6 and newer, one can use f-strings as well:

print(f'{firstname} {lastname}\n{houseNumber} {streetName} {cityName}\n{stateName}  {zipCode}')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using regex to count specific character in file shamishd 1 1,622 Oct-01-2021, 07:33 AM
Last Post: snippsat
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,610 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Replacing a few characters of a specified character but not all of them from a string fatherted99 7 3,222 Aug-13-2020, 09:08 AM
Last Post: fatherted99
  Change each character of list into an different characters Angry_bird89 1 2,060 Jun-19-2020, 08:29 AM
Last Post: Angry_bird89
  Search character from 2d list to 2d list AHK2019 3 2,500 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  Tab character in a string prints 8 spaces hecresper 6 20,603 Aug-27-2019, 02:38 PM
Last Post: snippsat
  Print strings enclosed with a character with different characters aylmaoxd 1 1,929 Aug-17-2019, 03:35 PM
Last Post: Resistance
  Finding and storing all string with character A at middle position Pippi 2 2,687 Jan-20-2019, 08:23 AM
Last Post: Pippi
  Remove special character from list vestkok 3 4,226 Nov-04-2018, 01:48 PM
Last Post: ichabod801
  unexpected character after line continuation character error newbie 10 14,660 Aug-09-2018, 06:07 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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