Python input() Function – A Complete Beginner’s Guide

💡 1. What Is the input() Function in Python?

The input() function is a built-in Python function used to receive information from the user through the keyboard.

✔ In simple words:

input() allows a Python program to ask the user for information.

⚙️ Let’s see a simple example:

name = input(“Enter your name: “)

▶ What happens when you run this statement?

1 Python displays the prompt:
Enter your name:
2 The program then waits for the user to type something.
3 Suppose the user enters:
Gopal
4 Python receives that value and stores it in the variable name.

📌 Key Point

The input() function makes a Python program interactive by allowing the user to provide their own information.

▶ 2. Your First Python input() Program

Let’s write our first interactive Python program. 😊

This program will ask the user to enter their name and then display a greeting.

💻 Python Code
name = input(“Enter your name: “)
print(“Hello”, name)

🔍 How does this program work?

1. Python displays the message "Enter your name: " on the screen.

2. The program waits for the user to enter a name.

3. The entered name is stored in the variable name.

4. The print() function then displays the greeting.

🖥️ Example Output
Enter your name: Gopal
Hello Gopal

📌 Key Point

The value entered by the user is stored in name, and we can use that variable later in the program.

🚀 Try It Yourself

Run the program in VS Code and enter your own name. Try entering a different name each time you run the program.

🧩 3. Basic Syntax of input()

🔹 1. The simplest form

The simplest way to use input() is:

input()

💬 2. Displaying a message to the user

We can place a message inside the parentheses to tell the user what to enter.

input(“Enter your name: “)

The text shown to the user is called a prompt.

⚙️ 3. General syntax

The general form of the function is:

input(“prompt”)
prompt → the message displayed to the user.

🎯 Example

Here, the prompt asks the user to enter their age:

input(“Enter your age: “)

When the program runs, the user sees:

Enter your age:

📌 Remember

The message inside input() is called the prompt. It helps the user understand what information the program is expecting.

🔍 4. Understanding the Prompt

Let’s take a closer look at this statement:

name = input(“Enter your name: “)

🧩 This statement has three important parts:

name — Variable
Stores the information entered by the user.
input() — Function
Receives information from the user through the keyboard.
"Enter your name: " — Prompt
The message shown to the user to tell them what to enter.

👀 Think of it this way

name = input() + “prompt”

Store the answer  •  Ask for input  •  Guide the user

📌 Key Point

The prompt is simply a message that tells the user what information the program is expecting.

📦 5. Storing User Input in a Variable

Usually, we want to use the information entered by the user later in the program.

For this reason, we store the value returned by input() in a variable.

💡 The basic idea

variable = input()

The value entered by the user is stored in the variable.

🎯 Example

Let’s ask the user for their name and store it in a variable called name.

name = input(“Enter your name: “)
print(“Your name is”, name)

🖥️ What happens when the program runs?

Suppose the user enters:

Enter your name: Gopal

Python stores Gopal in the variable name.

The second statement then uses that variable to display:

Your name is Gopal

📌 Important Idea

The statement name = input(...) means that the value entered by the user is assigned to name.

🔁 Using the variable again

Once the value is stored, we can use the variable multiple times later in the program.

name = input(“Enter your name: “)
print(“Hello”, name)
print(“Welcome”, name)
print(“Have a nice day,”, name)

Example output:

Enter your name: Gopal
Hello Gopal
Welcome Gopal
Have a nice day, Gopal

🌟 Remember

User input → is received by input() → and stored in a variable → so it can be used later.

🔤 6. What Type of Value Does input() Return?

⭐ Important for beginners

One of the most important things to remember about input() is that it returns the user’s input as a string.

Let’s understand this with a simple example.

age = input(“Enter your age: “)
print(age)
print(type(age))
👤 Suppose the user enters: 20

🖥️ Output

Enter your age: 20
20
<class ‘str’>

🔍 What does str mean?

str is short for string.

A string represents text in Python. Therefore, even though the user entered 20, input() has returned it as text.

💡 Remember the difference

User enters:
20
Python receives:
"20" → string

📌 Key Point

By default, input() always returns a string, even when the user enters a number.

➡️ Next: So, how can we use the user’s input as a number? That’s where int() and float() become useful.

🤔 7. Why Does This Matter?

Knowing that input() returns a string becomes very important when we want to perform calculations.

Suppose we write the following program:

a = input(“Enter first number: “)
b = input(“Enter second number: “)
print(a + b)

👤 The user enters:

Enter first number: 10
Enter second number: 20

💭 What might a beginner expect?

10 + 20 = 30

⚠️ But Python displays:

1020

🔍 Why does this happen?

Because the values returned by input() are strings.

So Python actually sees:

“10” + “20”

🧩 What does + do with strings?

With strings, the + operator joins the pieces of text together. This is called string concatenation.

“10” + “20” “1020”

Text + Text → Text

💡 The important difference

Strings
"10" + "20" → "1020"
Numbers
10 + 20 → 30
➡️ So how do we make Python treat the input as a number? We can convert the input using int() or float().

🔢 8. Taking Integer Input Using int()

In the previous section, we saw that input() returns the user’s input as a string.

If we want the input to be treated as a whole number (integer), we can use the int() function.

💡 Basic idea

age = int(input(“Enter your age: “))

Python first receives the input and then converts it into an integer.

🧮 Let’s solve the previous addition problem

This time, we will convert both inputs into integers before adding them.

a = int(input(“Enter first number: “))
b = int(input(“Enter second number: “))
print(a + b)

👤 Suppose the user enters

Enter first number: 10
Enter second number: 20

🔄 What does int() do?

“10” 10
“20” 20

String → Integer

🖥️ Output

Enter first number: 10
Enter second number: 20
30

🎯 Why is the result 30?

The inputs are converted before the addition takes place:

“10” 10 + “20” 20
10 + 20 = 30

📌 Remember

Use int(input()) when you want to take a user’s input as a whole number.

💡 Beginner Tip: int() is suitable for whole numbers such as 10, 25, 100. For decimal values such as 10.5, we will use float().

💰 9. Taking Decimal Input Using float()

What if the user needs to enter a number that contains a decimal point?

For example: 25.50

💡 Use float() for decimal numbers

The float() function converts the user’s input into a floating-point number.

🎯 Simple Example

price = float(input(“Enter price: “))
print(“Price =”, price)

Suppose the user enters:

Enter price: 25.50

Python converts 25.50 into a floating-point number and stores it in price.

🔄 What happens to the input?

“25.50” 25.50

String → Floating-point number

🧮 Practical Example – Calculate a Total

We can combine float() and int() when a program needs both decimal and whole-number input.

price = float(input(“Enter price: “))
quantity = int(input(“Enter quantity: “))
total = price * quantity
print(“Total =”, total)

👤 Example input:

Enter price: 25.50
Enter quantity: 4

🖥️ Output

Total = 102.0

📌 When should you use int() or float()?

🔢 `int()`
Use for whole numbers such as 10, 25, 100.
🔢 `float()`
Use for decimal numbers such as 10.5, 25.50, 3.14.

🌟 Remember

Use float(input()) when the user needs to enter a decimal number.

🧩 10. Taking Different Types of Input

So far, we have learned how to take text, whole numbers, and decimal numbers. Let’s combine them in one small program.

The program below asks the user for a name, age, and height.

💡 Notice the three types of input

📝 Text
input()
🔢 Whole number
int(input())
📏 Decimal
float(input())

💻 Python Code

name = input(“Enter your name: “)
age = int(input(“Enter your age: “))
height = float(input(“Enter your height in metres: “))
print(“Name:”, name)
print(“Age:”, age)
print(“Height:”, height)

🔍 What does each line do?

📝 name = input(...) → takes the user’s name as text.

🔢 age = int(input(...)) → takes the age and converts it to an integer.

📏 height = float(input(...)) → takes the height and converts it to a decimal number.

👤 Example

Suppose the user enters:

Enter your name: Gopal
Enter your age: 25
Enter your height in metres: 1.72

🖥️ Output

Name: Gopal
Age: 25
Height: 1.72

📌 Main Takeaway

Python lets us combine different input types in the same program. Choose the appropriate conversion based on the kind of data you expect from the user.

💡 Quick Reference: Text → input()   |   Whole number → int(input())   |   Decimal → float(input())

👋 11. Practical Example 1 – Greeting the User

Let’s create a simple interactive program that asks the user for their name and then displays a friendly greeting.

💻 Python Code

name = input(“Enter your name: “)
print(“Hello”, name)
print(“Welcome to Python!”)

🔍 How it works

1. The program asks the user to enter their name.

2. The entered name is stored in the variable name.

3. The program uses that variable to display a personalized greeting.

👤 Example

Enter your name: Gopal

🖥️ Output

Hello Gopal
Welcome to Python!

🌟 What did we learn?

We used input() to receive the user’s name and stored the result in a variable. The variable was then reused with print() to create a personalized message.

🚀 Try It Yourself

Change the program so that it also asks for the user’s favorite programming language and displays it in the greeting.

➕ 12. Practical Example 2 – Add Two Numbers

Let’s create a simple program that takes two numbers from the user and calculates their sum.

💻 Python Code

a = int(input(“Enter first number: “))
b = int(input(“Enter second number: “))
sum = a + b
print(“Sum =”, sum)

🔍 How does the program work?

1. int(input()) takes the first number and converts it to an integer.

2. The second number is taken in the same way and stored in b.

3. The statement sum = a + b adds the two numbers.

4. Finally, print() displays the result.

👤 Example

Suppose the user enters:

Enter first number: 25
Enter second number: 15

🧮 Calculation

25 + 15 = 40

🖥️ Output

Sum = 40

📌 Key Point

Since input() returns a string, we use int() to convert the user’s input into whole numbers before performing the addition.

🚀 Try It Yourself

Change the program so that it takes three numbers and calculates their total.

🧠 13. A Useful Rule to Remember

⭐ Before using input(), ask yourself:

❓ What kind of data do I want from the user?

📝 If it is text

name = input(“Enter your name: “)

➜ Use plain input() when you want text.

🔢 If it is a whole number

age = int(input(“Enter your age: “))

➜ Use int(input()) for whole numbers such as 18, 25, or 100.

📏 If it is a decimal number

price = float(input(“Enter price: “))

➜ Use float(input()) for decimal numbers such as 25.50 or 3.14.

⚡ Quick Decision Guide

Text? input()
Whole number? int(input())
Decimal number? float(input())

🌟 Remember This Simple Rule

Choose the conversion based on the type of data you expect. This simple habit will help you avoid many common beginner mistakes when working with input().

🏁 14. Complete Example

Let’s bring together several concepts we have learned in this tutorial and create one simple interactive program.

This program collects a student’s name, age, and favorite subject and then displays the information neatly.

💻 Python Code

name = input(“Enter your name: “)
age = int(input(“Enter your age: “))
favorite_subject = input(“Enter your favorite subject: “)
print()
print(“—– Student Information —–“)
print(“Name:”, name)
print(“Age:”, age)
print(“Favorite Subject:”, favorite_subject)

🧩 What concepts are used here?

📝 Text input
input()
🔢 Integer input
int(input())
📦 Variables
name, age
🖥️ Output
print()

👤 Example Input

Enter your name: Gopal
Enter your age: 20
Enter your favorite subject: Computer Science

🖥️ Output

—– Student Information —–
Name: Gopal
Age: 20
Favorite Subject: Computer Science

🔄 Program Flow

Ask for information Store in variables Display information

🌟 What did we learn?

This small program combines several important Python concepts: taking input, converting input, storing values in variables, and displaying the results.

🚀 Beginner Challenge

Modify the program to also ask for the student’s school name and display it in the Student Information section.

⚖️ 15. input() vs print()

Beginners often learn print() and input() together.

Although both are built-in Python functions, they have different purposes.

🔍 The Basic Difference

Function Purpose
print() 🖥️ Displays information on to the screen
input() ⌨️ Receives information from the user

🧠 Easy way to remember

input()
Gets information from the user.
print()
Sends information to the user.

💻 Example

name = input(“Enter your name: “)
print(“Hello”, name)

🔄 What happens here?

1. input() asks the user to enter information.

2. name stores the value entered by the user.

3. print() displays a message containing that value.

🔁 Think of them as two parts of an interaction

👤 User input() Python Program print() 🖥️ User

📌 Quick Revision

input() takes information in, while print() shows information out.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »