0

I am using Python for reference. I am trying to create a new line when displaying name, address, city, state, and zip code. However, when I do this, IDLE tells me that "unexpected character after line continuation character". My code is as follows:

name = input('What is your name? ') address = input('What is your street address? ') city = input('What city do you live in? ') state = input('What state do you live in? ') zip_code = input('What is your zip code? ') print(name\naddress) 

I know that I can print each thing separately but I want to know how I can print the result using one print function. I know I can do it if I were to have simple text such as: print('You have to\nshow up to class')

I am basically looking for the code to result as follows:

firstName lastName (first line)

address (second line)

city, state zip-code (third line)

Is there any way to input new lines before variables?

Any help would be appreciated!

3 Answers 3

1

I would do all my print statements using f-strings. It solves both the problems you stated

Example: Newline:

print(f"{name}\n{address}") 

Example: No spaces:

print(f"{city},{state},{zip_code}") 

Is this what you are looking for? The f-strings can be manipulated for many such variations of how you want your print output to look like.

UPDATE 1: With custom function or string concatenation

If you want to do this without using f-strings and conventional print function approach you can create your own custom print function. Here's just one of the many ways:

def prettyprint(*args): for arg in args: print(arg) prettyprint(name, address) 

Or you can add a new line while obtaining the input for you variables like so,

name = input('What is your name? ') + '\n' print(name+address) 

Or finally just combine it during print as,

print(name+'\n'+address) 

Not sure if any of this is what you need but thought will provide a few more options for you to explore.

Sign up to request clarification or add additional context in comments.

4 Comments

I haven't learned f strings just yet but thank you anyway! I don't think f works on Python the same as it does with other programming languages so it didn't affect my code. As for the second part, I figured it out! By putting in print(city+',', state, zip_code), I was able to omit that annoying extra space and keep it to the correct format.
print(name,'\n', address). Shouldn't it be done like this? (for your error)
Also maybe there is no way to new line with just variables, it seems that the way you said is the only way. I suppose my prof expected us to put in print 3 different times for 3 different lines, but I just wanted to see if there was a more efficient way. Thank you again!
What do you mean with variables?
0

You can use string formatting:

print("{}\n{}\n{}, {} {}".format(name, address, city, state, zip_code)) 

Comments

0

Here is your problem solution i have solved. If you want to use just one print function to print all over the statement you have to use f""#(f string). with \n and for spacing use \t.

name = input('What is your name? ') address = input('What is your street address? ') city = input('What city do you live in? ') state = input('What state do you live in? ') zip_code = input('What is your zip code? ') print(f"Name:{name}\nAddress:{address}\nCity:{city}\nState:{state}\nZip code:{zip_code}\n") print(f"{city}\t{state}\t{zip_code}") output: Name:Elon Address:25 sampur City:Washington D.C State:Alaska Zip code:99501 Washington D.C Alaska 99501 

Sorry for random Address

Comments