Getting the Answer in terms of Input()

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Input Terms
Click For Summary

Discussion Overview

The discussion revolves around a programming issue in Python related to string concatenation and user input. Participants explore how to correctly print a greeting that incorporates the user's name inputted via the `input()` function.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks help with a Python program that incorrectly outputs 'Hi, name' instead of 'Hi, x' where 'x' is the user's input.
  • Another participant points out the issue lies in the placement of quotes in the print statement.
  • A later reply explains the difference between a string literal and an expression involving a variable, emphasizing the need for proper concatenation.
  • Some participants introduce the concept of f-strings in Python 3.6 as a more efficient way to format strings with variable values.
  • Examples of both traditional concatenation and f-string usage are provided to illustrate the correct approach.

Areas of Agreement / Disagreement

Participants generally agree on the need to differentiate between string literals and variable expressions, but there are multiple approaches suggested for achieving the desired output.

Contextual Notes

Some participants reference specific versions of Python and string formatting techniques, indicating that the solution may depend on the programming environment being used.

WWGD
Science Advisor
Homework Helper
Messages
7,804
Reaction score
13,107
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:
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   Reactions: 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   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
A nice example of an f-string in python.
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
4
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K