🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #100

Python User Input


User Input

Python allows for user input.

That means we are able to ask the user for input.

The following example asks for your name, and when you enter a name, it gets printed on the screen:

Example

print("Enter your name:")
name = input()
print(f"Hello {name}")

Note: Python stops executing when it comes to the input() function, and continues when the user has given some input.


Using prompt

In the example above, the user had to input their name on a new line. The Python input() function has a prompt parameter, which acts as a message you can put in front of the user input, on the same line:

Example

name = input("Enter your name:")
print(f"Hello {name}")

Multiple Inputs

You can add as many inputs as you want, Python will stop executing at each of them, waiting for user input:

Example

name = input("Enter your name:")
print(f"Hello {name}")
fav1 = input("What is your favorite animal:")
fav2 = input("What is your favorite color:")
fav3 = input("What is your favorite number:")
print(f"Do you want a {fav2} {fav1} with {fav3} legs?")

Input Number

The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string.

You can convert the input into a number with the float() function:

Example

x = input("Enter a number:")

#find the square root of the number:
y = math.sqrt(float(x))

print(f"The square root of {x} is {y}")

Validate Input

It is a good practice to validate any input from the user. In the example above, an error will occur if the user inputs something other than a number.

To avoid getting an error, we can test the input, and if it is not a number, the user could get a message like "Wrong input, please try again", and allowed to make a new input:

Example

y = True
while y == True:
  x = input("Enter a number:")
  try:
    x = float(x);
    y = False

except:
    print("Wrong input, please try again.")

print("Thank you!")

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python User Input – FAQs

Quick answers about learning Python User Input in Python.

This free note from CodingNow 2.0 explains Python User Input in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python User Input, is 100% free with no signup required.
With focused practice, most students grasp Python User Input in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now