Ask coding questions

← Back to all posts
How to add a input in python
AmalinaJamil (1)

I don't know how to add an input in python because i'm a beginner.
please help

Muffinlavania (1526)

Use @UniqueOstrich18 for the answer, but just use input() like he said
Put whatever variable you want before it, and in the paraenthesis, put what you want to ask in quotes, like this:
Your_Variable = input("Ask your Question Here")

Then you can call this variable later like @UniqueOstrich18 said,
You just either print it like this:


Or add text before it, and add a plus sign then your variable
Like this:
print("The Answer to my question was"+Your_Variable)

UniqueOstrich18 (331)

You can use the input() statement:

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

If this was helpful, please mark it as the answer.

multicoder2021 (3)

You can also do:

inpute = input('What is your name?')
print('You r %a years old' % age)
multicoder2021 (3)

Everyone's given the solution, mark at least one as the answer...

It's simple:

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

Or, if you want to use Python 2,

g = raw_input("Enter your name : ")
print g

You can also add a 'str()' or 'int()' to specify what data type is allowed:

name = str(input('Name?  '))
age =  int(input('Age?  '))

It will give an error if you type the wrong data type (integer or string)

multicoder2021 (3)

Exactly what both of them said, use the input() function.
You can also specify if it can be an integer or string

number = int(input("Enter a number: "))
print("The number is : " + number)
string = str(input("Enter a string: ")
print("You entered '" + string + "'")

Also, a suggestion:
Instead of printing the variable directly, or doing it like this:
print("TEXT" , variable_name)
You can use the .format() method or use f-strings.
Example of the .format():

x = int(input("Enter an integer:"))
print("The number is {}".format(x))

Example of f-strings [I prefer this method]:

x = int(input("Enter an integer")
print(f" The number is {x}")