Python Forum

Full Version: Unexpected character after line continuation character
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)
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.
(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))
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.
If using 3.6 and newer, one can use f-strings as well:

print(f'{firstname} {lastname}\n{houseNumber} {streetName} {cityName}\n{stateName}  {zipCode}')