Python input() Function

User input is an important part of Python applications. The input() function can be used to get user input interactively in Python scripts and applications. The input() function simply reads the user input via the standard input and returns the value which is generally assigned to a string variable.

input() Function Syntax

The input() function has the following syntax. The input() function returns the input value as string type.

input(MESSAGE)
  • MESSAGE is the message which is displayed before the user input.

input() Function Example

In the following example, we get user input for the username. The input value is assigned into the name a variable which is a string.

name = input("Please enter your name:")

print("Your name is ",name)
Your name is  İsmail

Convert User Input

As a generic type user input is returned as a string. But in some cases, this may not be the result we expect. We can convert the user input into other types like integers. In the following example, we request the age information and convert provided age value into an integer with the int() function.

i = input("Please enter your age:")

age = int(i)

print("You will be ",age+20," after 20 years.")
You will be  58  after 20 years.

Input Multiple Items As List

Another use case for the input() function is the ability to get multiple items with a single input. The input values can be converted into a list easily by using the split() . In the following example, we provide “ahmet ali baydan” as input and split it according to spaces.

l = input("test:").split()

print(l)
['ahmet', 'ali', 'baydan']

Leave a Comment