Python Input

Input

The input function is used to get input from the user in string data type (If the user enters [1,2,3], it will be "[1,2,3]" – it becomes a string, not a list)

Example

age = input(“Enter your age: “) # Prompt the user to enter their age; the input is returned as a string
print(“Your age is: “, age) # Print the age entered by the user

age = input("Enter your age: ")
print("Your age is: ", age)

Result

What is your age? 40
Your age is: 40

You can also have that in a loop

Example

temp_var = “” # Initialize an empty string variable
while temp_var != “exit”: # Continue looping until the user types “exit”
    temp_var = input(“Enter text: “) # Prompt the user to enter text
    print(“You entered: “, temp_var) # Print the text entered by the user

temp_var = ""
while temp_var != "exit":
temp_var = input("Enter text: ")
print("You entered: ", temp_var)

Result

Enter text: 10
You entered: 10
Enter text: test
You entered: test
Enter text: exit
You entered: exit

Also, you can check the length

Example

temp_var = “” # Initialize an empty string variable
while len(temp_var) != 4: # Repeat the loop until the user enters a string of length 4
    temp_var = input(“Enter a number: “) # Prompt the user to enter a number
    print(“You entered: “, temp_var) # Print the value entered by the user

temp_var = ""
while len(temp_var) != 4:
temp_var = input("Enter a number: ")
print("You entered: ", temp_var)

Result

Enter a number: a
You entered: a
Enter a number: bb
You entered: bb
Enter a number: ccc
You entered: ccc
Enter a number: dddd
You entered: dddd

Input (Type)

The input function returns a string, and you can check that using the type function

Example

temp_var = input(“Enter a number: “) # Prompt the user to enter a number; input is always returned as a string
print(type(temp_var)) # Print the type of temp_var

temp_var = input("Enter a number: ")
print(type(temp_var))

Result

Enter a number: 40
<class 'str'>

Input (Casting or Converting to int)

To cast, or convert a string into an int, you can use the int function

Example

temp_var = input(“Enter a number: “) # Prompt the user to enter a number; input is returned as a string
temp_var = int(temp_var) # Convert the input string to an integer
print(type(temp_var)) # Print the type of temp_var

temp_var = input("Enter a number: ")
temp_var = int(temp_var)
print(type(temp_var))

Result

Enter a number: 40
<class 'int'>

Input (Safe Casting or Converting)

Sometimes, functions that evaluate a string into code could be exploited, so it’s recommended that you use safe eval functions such as literal_eval from ast module (If needed)

Example

import ast # Import the Abstract Syntax Trees module (used here for safe evaluation)
temp_var = input(“Enter a float number: “) # Prompt the user to enter a number; input is returned as a string
temp_var = ast.literal_eval(temp_var) # Safely evaluate the input to its Python type (int, float, etc.)
print(type(temp_var)) # Print the type of temp_var

import ast
temp_var = input("Enter a float number: ")
temp_var = ast.literal_eval(temp_var)
print(type(temp_var))

Result

Enter a number: 40.0
<class 'float'>

Sanitizing Input

If you are expecting input that does not contain specific characters, you need to sanitize the input (Do not rely on the user to input something without the specific characters)

Example

temp_var = input(“Enter a string that does not contain @: “) # Prompt the user to enter a string
temp_var = temp_var.replace(“@”, “”) # Remove all occurrences of “@” from the string
print(temp_var) # Print the modified string

temp_var = input("Enter a string that does not contain @: ")
temp_var = temp_var.replace("@", "")
print(temp_var)

Result

Enter a number: Hello World!@
Hello World!