Python Basics
Python User Input
Getting user input
Python User Input
Python allows for user input. That means we are able to ask the user for input. The method is a bit different in Python 3.6 than Python 2.7. Python 3.6 uses the input() method. Python 2.7 uses the raw_input() method.
User Input
Python 3.6 uses the input() method:
username = input("Enter username:")
print("Username is: " + username)
Python 2.7
Python 2.7 uses the raw_input() method. The following example asks for the username, and when you entered the username, it gets printed on the screen:
username = raw_input("Enter username:")
print("Username is: " + username)
Note: Python 2.7 is no longer maintained. Python 3.6 is recommended.
String Input
By default, input() returns a string. If you want to get a number, you need to convert it:
age = input("Enter your age: ")
age = int(age)
print("You are", age, "years old")
Multiple Inputs
You can get multiple inputs in one line:
name, age = input("Enter your name and age: ").split()
print(f"Name: {name}, Age: {age}")