π· What is the input() Function in Python?
Hi! My name is Safia Khatoon. I am complete my Bachelors in Technology from RTC Institute Of Technology. My specialisation in Computer Science and Engineering.I love contributing to Open Source with the help of the skills I gain.
Also, I'm working on my YouTube Channel as well where I teach about DevOps tools and make technical content. You can have a look at it through my profile.
Feel free to reach out to me! I'd be happy to connect with you.
π’ Purpose of input() function:
β‘οΈ To take input from the user (via keyboard)
π Whenever you want the user to enter something (like name, number, or a choice), use input().
π Syntax:
input("Write your prompt message here")
π§ Important: Whatever value you get from input() is always a string (i.e., text) by default.
πΈ Basic Example:
name = input("Enter your name: ")
print("Hello", name)
π¨βπ» Output:
Enter your name: Safia
Hello Safia
πΈ Want to Take Numbers? You Have to Convert Them!
π Remember: input() gives you everything as a string
So, if you want a number, you must convert it using int() or float()
β For integers:
age = int(input("Enter your age: "))
print("Your age is:", age)
β For decimals:
height = float(input("Enter your height in cm: "))
print("You are", height, "cm tall")
πΈ Taking Multiple Inputs on One Line
β‘οΈ Use split() function for that!
π§ͺ Example:
a, b = input("Enter two numbers: ").split()
print("You entered:", a, "and", b)
π¨βπ» Input:
Enter two numbers: 5 10
π Output:
You entered: 5 and 10
πΈ String + Number Input Combo
name = input("Enter your name: ")
marks = float(input("Enter your marks: "))
print(name, "got", marks, "marks.")
π§ Pro Tip: Use input() whenever your program needs user interaction
β Use Cases:
Getting name, age, email, or password
Building a calculator
Filling a form
Getting user choice in a game
π― Final Recap Table:
| Concept | Example Code | Output Example |
| Simple input | input("Enter name: ") | Safia |
| Number input | int(input("Enter age: ")) | 21 |
| Float input | float(input("Enter height: ")) | 155.5 |
| Multi input | input().split() | 10 20 |