Skip to content
Advertisement

How to create a new line when only using n with variables rather than printed text?

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(namenaddress)

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 tonshow 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!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement