Learn how to use Python's input() function effectively! Get user input, handle data types (int, float, string), add prompts, and validate input with practical examples.
input()
?The input()
function in Python is used to take user input from the keyboard. It allows a program to interact with users by asking for information.
variable_name = input("Prompt message")
Prompt message
: A string displayed to the user before they enter input.variable_name
: The variable where the user’s input is stored.Basic Example
name = input("Enter your name: ")
print("Hello, " + name + "!")
input("Enter your name: ")
displays the message “Enter your name: “ and waits for the user to type something.name
.print("Hello, " + name + "!")
displays a greeting message with the user’s name.By default, input()
returns a string. If you need a number, you must convert it using int()
or float()
.
age = int(input("Enter your age: "))
print("In 5 years, you will be", age + 5)
int(input(...))
converts it into an integer.