Getting the Answer in terms of Input()

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Input Terms
AI Thread Summary
The discussion centers around a programming issue where the user is trying to print a greeting using input from the user. The user initially encounters a problem where the output displays 'Hi, name' instead of 'Hi, x' after inputting their name. The solution involves understanding the difference between string literals and variable expressions in Python. The correct approach is to concatenate the greeting with the user's input, using either the '+' operator or an f-string for Python 3.6 and above. Examples provided include using `print('Hi ' + name)` and `print(f'Hi {name}')`, which correctly display the greeting with the user's name. The conversation also touches on similar string handling features in other programming languages.
WWGD
Science Advisor
Homework Helper
Messages
7,700
Reaction score
12,717
TL;DR Summary
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:
Technology news on Phys.org
WWGD said:
the output is 'Hi , name' , and not 'Hi x'

Look at where the quotes are in your print statement.
 
  • Like
Likes QuantumQuest and WWGD
PeterDonis said:
Look at where the quotes are in your print statement.
Thanks, Peter, got it.
 
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 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 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 jedishrfu and WWGD
A nice example of an f-string in python.
 
Back
Top