Home > Back-end >  Python 3: How do I add input and a string to the same line?
Python 3: How do I add input and a string to the same line?

Time:03-04

I have to put the input on the same line as the string and can't figure out how. Here's the code:

print('Hello! My name is Awesome Computer.')
print('What is your name?')
name = input()
print('It is good to meet you '   name   '.')
print('How are you today?')
input()
print('Okay.')
print('I am doing great!')

CodePudding user response:

The function input() takes in a string to print so you can do this:

name = input("What is your name? ")

And it will print the string before taking input without adding a newline

CodePudding user response:

You can put a string into the input function parameters like I did below and it will print the string and get the input on the same line. Also, your second input function call wasn't being saved into a variable.

print('Hello! My name is Awesome Computer.')
name = input('What is your name? ')
print('It is good to meet you '   name   '.')

# don't forget to save your input into a variable 
mood = input('How are you today? ')
print('Okay.')
print('I am doing great!')

CodePudding user response:

print('Hello! My name is Awesome Computer.')
# print('What is your name?')
name = input('What is your name? ')
print('It is good to meet you '   name   '.')
# print('How are you today?')
input('How are you today? ')
print('Okay.')
print('I am doing great!')

Test Results

Hello! My name is Awesome Computer.
What is your name? John Doe
It is good to meet you John Doe.
How are you today? Healthy
Okay.
I am doing great!
  • Related