Help with new code

Hello, I’m brand new to coding and learning python for my class. I’m currently trying to write a simple name=input code, but no matter what I do I can’t make my third line print the name that I put into the input. Is there something wrong with my code? I have my code set up as follows:

name = input('what is your name?')
print('hello')
print('name')

Sam,

It is a common early misunderstanding.

You want to print not the characters in the string ‘name’ but the contents of the variable name. Skip the quotes like this:

name = input('what is your name?')
print('hello')
print(name)

Of course, if you want to print them on the same line, perhaps with a space between and a period after, …

No worries Sam, We all struggled with this.
by putting name in quotations like this ‘name’ that’s printing exactly what you put in quotes that’s why it was printing name instead of whatever you assigned to the name variable, by putting name without quotes that is telling it that you want to print whatever is equal to the name variable.

To sum up putting something in quotes most of the time is telling python to print exactly that. ( Usually this is the case but when working with modules it can be different ).

Avi gave you the fixed code but you could shorten this by doing

name = input('what is your name? ')
print('hello ',name)

what it’s doing is adding a variable to the print statement
you could also use a ‘+’ symbol instead of a ‘,’ both work fine.

When you start programming, it is often good to do things a step at a time until you get comfortable. Then, all kinds of alternate or shorter ways are possible.

In your code, if you only want to use the name once you can even avoid creating a permanent variable name to hold it and can shorten it even more to something like this:

print('hello ', input('what is your name? '))

But if you will need to use the name again in the program, perhaps best not to leave the anonymous variable. Somewhat more advanced people may use the walrus operator like so:

>>> print('hello ', name := input('what is your name? '))
what is your name? Avious
hello  Avious
>>> print(name)
Avious

But such conciseness is a dangerous temptation if it makes reading and modifying your code harder.

1 Like

Just to give another alternative:

name = input('what is your name?')
print(f'Hello {name}.')

Will use the f-string formatting

As a one liner if you don’t need the name multiple times:

print(f'Hello {input("what is your name?")}')

Do note the " in the input in the one-liner. If you’re using Python < 3.12 you need to use a different quotation mark inside the f-string than you do outside of it. This was fixed in 3.12.

name = input('what is your name?')
print('hello') # This prints the string "hello"
print(name) # This prints the CONTENTS of the variable called 'name'.

# Below prints the string "Hello" combined with the contents of the variable "name" in one line. This is called an f-string. You might cover f-strings later in your class. They are very handy.
print(f"Hello {name}") 

# Here's another way to print a string with a variable. Python automatically adds a space when you use a comma.
print("Hello",name)

# Now practice changing the value of the variable "a". Notice what prints out.
a = 1
print(a)
a = 2
print(a) 
1 Like