Getting the Answer in terms of Input()

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Input Terms
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 2K views
Messages
7,828
Reaction score
13,156
TL;DR
Program asks for input from user. I want to get a printout using input() value.
Hi, I have this mini-program and I am being lazy. Wherever I post this they make me go through a grinder reviewing basics . I just need a small hint. We are asking for the name of the user in "name =input()". I would like the line immediately after name 'x' is inputed to print 'Hi, x '. Instead, when I input 'x' as the name, the output is 'Hi , name' , and not 'Hi x':

Python:
('Hi, what is your name?')
name=input()
print('Hi + name' )
 
Last edited by a moderator:
Physics news on Phys.org
Some languages have smart strings where you can say “hi $name” and the value of the name variable will replace the $name token. One example are bash scripts and another is the groovy language. I think python might support this behavior in v3.6 with an f-string.

https://realpython.com/python-f-strings/
 
  • Like
Likes   Reactions: QuantumQuest and WWGD
From post #1, mostly unchanged:
Code:
print ('Hi, what is your name?')
name=input()
print('Hi + name')
In any programming language, it's very important to understand the difference between a string literal and an expression involving a literal and a variable. There is a difference between 'Hi + name', which is a string of characters, and 'Hi' + name. The latter is the concatenation of the string 'Hi' with the value of the variable called name.

Here's a slightly different version of your program that uses the input() command to issue a prompt, and also concatenates 'Hi ' with the string entered by the user.
Python:
name = input('Hi, what is your name? ')
out = 'Hi ' + name
print(out)
 
  • Like
Likes   Reactions: QuantumQuest, jedishrfu and WWGD
And here is another version for Python >= 3.6 which is closer to your original
Python:
name = input('Hi, what is your name?')
out = f'Hi {name}'
print(out)
 
  • Like
Likes   Reactions: jedishrfu and WWGD