# Day 10 – Taking User Input in Python

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/1f6322ae-99be-4c1a-91ee-001713b1ff9c.jpg align="center")

# Day 10 – Taking User Input in Python

Welcome to **Day 10 of 100 Days of Python!**

Until now, most of our programs have worked with values that we directly wrote inside the code.

But what if we want the **user to provide the data while the program is running?**

That's where Python's built-in `input()` **function** comes in.

In this lesson, we will learn:

*   How to take input from a user
    
*   How `input()` works
    
*   Why `input()` always returns a string
    
*   How to take integer and float input
    
*   How to use a prompt with `input()`
    
*   How type casting works with user input
    
*   A common mistake when performing calculations with input
    

* * *

# 1\. What is User Input?

**User input** is data provided by a user while a program is running.

Python provides the built-in `input()` function to take input from the user.

The basic syntax is:

```python
variable = input()
```

For example:

```python
name = input()

print(name)
```

If the user enters:

```text
Sritesh
```

The output will be:

```text
Sritesh
```

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

* * *

# 2\. How Does `input()` Work?

When Python encounters `input()`, the program pauses and waits for the user to enter something.

For example:

```python
name = input()

print("Hello, " + name)
```

If the user enters:

```text
Sritesh
```

The program produces:

```text
Hello, Sritesh
```

So the basic flow is:

```text
Program
   ↓
input()
   ↓
User enters data
   ↓
Python receives the data
   ↓
Value is stored in a variable
```

* * *

# 3\. Important: `input()` Always Returns a String

This is one of the most important things to remember about `input()`:

> **The** `input()` **function always returns the user's input as a string (**`str`**).**

For example:

```python
age = input()

print(age)
print(type(age))
```

If the user enters:

```text
23
```

The output will be:

```text
23
<class 'str'>
```

Even though we entered `23`, Python received it as:

```python
"23"
```

not:

```python
23
```

This becomes very important when we want to perform calculations.

* * *

# 4\. Taking Integer Input

If we need an integer from the user, we can use `int()` with `input()`.

```python
age = int(input())

print(age)
print(type(age))
```

If the user enters:

```text
23
```

Output:

```text
23
<class 'int'>
```

Here:

```python
input()
```

takes the input as a string, and:

```python
int()
```

converts that string into an integer.

The process is:

```text
User enters 23
      ↓
input()
      ↓
"23"
      ↓
int()
      ↓
23
```

* * *

# 5\. Taking Float Input

We can also take decimal values using `float()`.

```python
price = float(input())

print(price)
print(type(price))
```

If the user enters:

```text
99.50
```

Output:

```text
99.5
<class 'float'>
```

The general pattern is:

```python
integer_value = int(input())
float_value = float(input())
string_value = input()
```

* * *

# 6\. Displaying a Message with `input()`

We don't have to leave `input()` empty.

We can provide a message inside the parentheses.

This message is called the **prompt**.

Example:

```python
name = input("Enter your name: ")

print(name)
```

The user will see:

```text
Enter your name: Sritesh
```

After entering the name, the program prints:

```text
Sritesh
```

The syntax is:

```python
variable = input("Prompt message")
```

* * *

# 7\. Creating a Greeting Program

We can combine `input()` with string concatenation to create a simple interactive program.

```python
name = input("Enter your name: ")

print("Hello, " + name + "!")
```

Example:

```text
Enter your name: Sritesh
Hello, Sritesh!
```

This is more useful than hard-coding the name:

```python
print("Hello, Sritesh!")
```

because now the program can greet different users.

* * *

# 8\. Taking Multiple Inputs

We can take multiple values from the user and store them in different variables.

For example:

```python
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

print("Hello,", first_name, last_name)
```

Example:

```text
Enter your first name: Sritesh
Enter your last name: Suranjan
Hello, Sritesh Suranjan
```

* * *

# 9\. A Common Mistake with Numbers

Consider this program:

```python
a = input("Enter first number: ")
b = input("Enter second number: ")

print(a + b)
```

Suppose the user enters:

```text
10
20
```

You might expect:

```text
30
```

But the output is:

```text
1020
```

Why?

Because `input()` returns strings.

Python is actually performing:

```python
"10" + "20"
```

For strings, `+` means **concatenation**, not mathematical addition.

So:

```text
"10" + "20"
      ↓
"1020"
```

* * *

# 10\. Converting User Input to Integers

To perform numerical addition, we need to convert the input into integers.

```python
a = input("Enter first number: ")
b = input("Enter second number: ")

print(int(a) + int(b))
```

If the user enters:

```text
10
20
```

The output is:

```text
30
```

We can also convert the values while taking the input:

```python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print(a + b)
```

This is a very common pattern in Python.

* * *

# 11\. Practical Example

Let's combine everything we have learned so far.

```python
name = input("Enter your Name: ")
print("Hello, " + name + "!")

first_number = input("Enter first number: ")
second_number = input("Enter second number: ")

print(first_number + second_number)
print(int(first_number) + int(second_number))
```

Example:

```text
Enter your Name: Sritesh
Hello, Sritesh!
Enter first number: 10
Enter second number: 20
1020
30
```

The first result:

```text
1020
```

is string concatenation.

The second result:

```text
30
```

is numerical addition after type conversion.

This example connects directly with what we learned on **Day 9 – Type Casting in Python**.

* * *

# 12\. `input()` with Different Data Types

Here are some common patterns:

### String

```python
name = input("Enter your name: ")
```

### Integer

```python
age = int(input("Enter your age: "))
```

### Float

```python
price = float(input("Enter the price: "))
```

Remember:

```python
input()
```

itself always produces a string.

The conversion happens because we explicitly use:

```python
int()
```

or:

```python
float()
```

* * *

# 13\. Complete Program

Here is the complete program for today's lesson:

```python
a = input("Enter your Name: ")
print("Hello, " + a + "!")

b = input("Enter first number: ")
c = input("Enter second number: ")

print(b + c)
print(int(b) + int(c))
```

Example output:

```text
Enter your Name: Sritesh
Hello, Sritesh!
Enter first number: 10
Enter second number: 20
1020
30
```

* * *

# 14\. Important Things to Remember

### `input()` always returns a string

```python
value = input()
print(type(value))
```

Output:

```text
<class 'str'>
```

### Use `int()` for integers

```python
age = int(input())
```

### Use `float()` for decimal numbers

```python
price = float(input())
```

### Use a prompt to guide the user

```python
name = input("Enter your name: ")
```

### Don't forget type conversion for calculations

```python
a = int(input())
b = int(input())

print(a + b)
```

* * *

# 15\. Quick Revision

```text
                User Input
                    │
                    ▼
                 input()
                    │
                    ▼
              Always returns
                 a string
                    │
          ┌─────────┴─────────┐
          ▼                   ▼
       int()                float()
          │                   │
          ▼                   ▼
       Integer              Float
```

### Remember These:

```python
name = input("Enter your name: ")

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

price = float(input("Enter the price: "))
```

And remember the classic beginner example:

```python
"10" + "20"           # "1020"

int("10") + int("20") # 30
```

* * *

# 16\. Key Takeaways

After completing Day 10, you should understand:

*   What user input is
    
*   How to use Python's `input()` function
    
*   How to store user input in variables
    
*   Why `input()` always returns a string
    
*   How to take integer input using `int()`
    
*   How to take decimal input using `float()`
    
*   How to display a prompt to the user
    
*   Why `"10" + "20"` produces `"1020"`
    
*   How type casting allows us to perform numerical calculations
    
*   How to create a simple interactive Python program
    

The `input()` function is an important step because our programs are no longer limited to values written directly in the source code. We can now make programs that **interact with users and respond to the data they provide**.

* * *

## **📂 Day 10 Resources**

All notes and code for this day are available in the GitHub repository:

%[https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/10-Day10-Taking-User-Input] 

* * *
