Skip to main content

Command Palette

Search for a command to run...

πŸ”· What is the input() Function in Python?

Published
β€’2 min readβ€’View as Markdown
S

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:

ConceptExample CodeOutput Example
Simple inputinput("Enter name: ")Safia
Number inputint(input("Enter age: "))21
Float inputfloat(input("Enter height: "))155.5
Multi inputinput().split()10 20

More from this blog

Untitled Publication

136 posts