Python User Input – Taking Input from the User

Python allows you to interact with users by taking keyboard input using the input( ) function.



Basic Syntax

input("Your message here")

It always returns the input as a string by default.



Example 1: Take a name from the user

name = input("Enter your name: ")
print("Hello,", name)

#Output - 
Enter your name: Rishik
Hello, Rishik


Example 2: Take a number (convert to int)

age = input("Enter your age: ")
age = int(age)  # Convert to integer
print("Next year, you will be", age + 1)

#Output -
Enter your age: 25
Next year, you will be 26

Example 3: Multiple Inputs (Using split( ))

name, age = input("Enter your name and age: ").split()
print("Name:", name)
print("Age:", age)

#Output - 
Enter your name and age: Alice 25
Name: Alice
Age: 25


Important Points


Feature Details
input() Takes user input (returns as string)
int(input()) Converts input to integer
float(input()) Converts input to float
input().split() Splits input into multiple values
input().upper() Converts input to uppercase


Example 4: Safely Taking Integer Input

try:
    number = int(input("Enter a number: "))
    print("Double of your number:", number * 2)
except ValueError:
    print("Please enter a valid number!")


Summary




Practice Python Code Here: