Flat-style illustration titled Python input and output explained showing lowercase input(), print(), and f-string examples with a laptop, speech bubbles, and Python code on a blue background.

Python Input and Output Explained: input(), print(), and f-strings

Estimated reading time: 3.3 mins read

Interaction is what makes a program useful and dynamic. Instead of hard-coding values, Python allows users to enter data at runtime and displays meaningful output based on that input.


Output in Python using print()

What is print()?

The print() function is used to display output on the screen.

Basic Example:

# Printing a simple message
print("Welcome to Python")

Printing Variables:

name = "Nishtha"

# Printing variable value
print(name)

Taking User Input using input()

What is input()?

The input() function allows the user to enter data from the keyboard.
⚠️ By default, input() always returns string data.

Example:

# Taking input from user
name = input("Enter your name: ")

# Printing the input
print(name)

📝 Whatever the user types is stored in the variable name.


Understanding the Problem with input()

Even if the user enters a number, Python treats it as a string.

Example:

age = input("Enter your age: ")

print(age)
print(type(age))

✅ Output:

25
<class 'str'>

✔ This is why type conversion is important.


Type Conversion in Python

Why Type Conversion?

To perform calculations, input values must be converted into numeric types.


🔹 Converting to Integer using int()

# Taking numeric input and converting to integer
age = int(input("Enter your age: "))

# Adding 1 to age
print(age + 1)

🔹 Converting to Float using float()

# Taking decimal input
price = float(input("Enter product price: "))

# Adding tax
print(price + 10.5)

Printing Multiple Values

Python allows printing multiple values in a single print() statement.

Example:

name = "Nishtha"
age = 25

# Printing multiple values
print(name, age)

Python automatically adds a space between values.


# This works only when both are strings
print("Age is " + str(age))

⚠️ Requires manual conversion and is less readable.


f-strings (Best and Professional Way)

What are f-strings?

f-strings provide a clean, readable, and modern way to format output.
They are widely used in real projects and interviews.


Basic f-string Example:

name = "Nishtha"
age = 25

print(f"My name is {name} and I am {age} years old")

✔ No need to convert data types
✔ Easy to read
✔ Best practice


f-string with Calculations:

a = 10
b = 5

print(f"Sum of {a} and {b} is {a + b}")

Real-World Input Examples

✅ Example 1: Login Name Display

username = input("Enter username: ")

print(f"Welcome, {username}!")

✅ Example 2: Age Validation

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

if age >= 18:
    print("You are eligible to vote")
else:
    print("You are not eligible to vote")

✅ Example 3: Simple Bill Calculator

item_price = float(input("Enter item price: "))
quantity = int(input("Enter quantity: "))

total = item_price * quantity

print(f"Total bill amount is: {total}")

✔ Used in real billing systems
✔ Common interview example


Common Beginner Mistakes

❌ Forgetting type conversion
❌ Using + with string and number
❌ Not understanding that input() returns string
❌ Not using f-strings for formatting


Important Interview Qs & As

1.What does input() return in Python?

input() always returns data in string format.

2.Why is type conversion required with input()?

Because arithmetic operations cannot be performed on string values.

3.How do you convert user input into an integer?

int(input(“Enter number: “))

4.What is an f-string?

An f-string is a formatted string used to embed variables and expressions directly inside a string.

5.Why are f-strings preferred over +?

They are more readable, cleaner, and do not require manual type conversion.

6.Can we perform calculations inside f-strings?

Yes, expressions can be evaluated inside {}.

7.What happens if invalid input is given to int()

Python raises a ValueError.

8.Where are input and output used in real projects?

User forms, login systems, billing applications, automation scripts, and testing validations.

Happy Learning !


Comments

Leave a Reply

Discover more from Learn With Nishtha

Subscribe now to keep reading and get access to the full archive.

Continue reading