Paramclasses Academy - Blog

Tutorials Details

←
Post 4 of 20
→

Python Input and Output: Using input(), print(), Type Conversion & Formatting

Python Input and Output: Using input(), print(), Type Conversion & Formatting

October 16, 2026

Python Input and Output: Using input(), print(), Type Conversion & Formatting

Introduction to Python Input and Output

Input and output are fundamental concepts in Python programming. Input allows users to provide data to a program, while output displays information or results on the screen.

Python provides built-in functions such as input() and print() to handle basic input and output operations. In this tutorial, you will learn how to accept user input, display output, convert data types, and format text using simple Python examples.

1. The print() Function in Python

The print() function displays text, variable values, and calculation results on the screen.

Example:

print("Hello, World!")
print("Welcome to Python Programming")

name = "Alex"
age = 25

print(name)
print(age)
print("Name:", name, "Age:", age)

Output:

Hello, World!
Welcome to Python Programming
Alex
25
Name: Alex Age: 25

You can use print() to display messages, debugging information, and program results.

Using sep and end Parameters

The sep parameter specifies the separator between multiple values, while end specifies what is printed at the end of the output.

print("Python", "PHP", "JavaScript", sep=" | ")
print("Hello", end=" ")
print("World")

Output:

Python | PHP | JavaScript
Hello World

2. The input() Function in Python

The input() function accepts data entered by the user through the keyboard. It returns the entered value as a string.

Example:

name = input("Enter your name: ")

print("Welcome,", name)

If the user enters Alex, the output will be:

Enter your name: Alex
Welcome, Alex

The program stores the entered name in the name variable and displays a welcome message.

3. Taking Multiple Inputs

You can ask users to enter multiple values using separate input() statements.

name = input("Enter your name: ")
city = input("Enter your city: ")

print("Name:", name)
print("City:", city)

You can also split a single line of input into multiple values.

name, city = input("Enter name and city: ").split()

print("Name:", name)
print("City:", city)

For the second example, enter the name and city separated by a space, such as Alex Raipur.

4. Type Conversion with User Input

An important point to remember is that input() returns a string, even when the user enters a number. You must convert the input to another data type when performing numerical calculations.

Common type conversion functions include:

  • int() – Converts a suitable value to an integer.

  • float() – Converts a suitable value to a floating-point number.

  • str() – Converts a value to a string.

  • bool() – Converts a value to a Boolean.

Example:

age = int(input("Enter your age: "))
height = float(input("Enter your height in meters: "))

print("Age:", age)
print("Height:", height)

If a user enters an invalid number, such as abc when an integer is expected, conversion will raise a ValueError. You can handle this using exception handling.

5. Performing Calculations with User Input

Let's create a simple program that accepts two numbers and displays their sum.

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

total = num1 + num2

print("Sum:", total)

Example output:

Enter the first number: 10
Enter the second number: 20
Sum: 30.0

Using float() allows the program to accept both whole numbers and decimal values.

6. Formatting Output in Python

Python provides several ways to format output and make it easier to read.

Using f-Strings

F-strings allow you to insert variables and expressions directly into a string.

name = "Alex"
age = 25

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

Output:

My name is Alex and I am 25 years old.

Formatting Decimal Values

You can use formatting options to control the number of decimal places displayed.

price = 99.9876

print(f"Price: {price:.2f}")

Output:

Price: 99.99

The .2f format displays the number to two decimal places.

Using format()

Python also supports the format() method.

name = "Alex"
score = 95

print("Student: {}, Score: {}".format(name, score))

7. Practical Example: Simple User Information Program

The following program collects basic information and displays it in a readable format.

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

print("\n--- User Information ---")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"City: {city}")

This example combines user input, type conversion, and formatted output.

8. Practice Questions

Test your understanding by solving these questions:

  1. What is the purpose of the print() function?

  2. What does the input() function return by default?

  3. Why do we use int() and float() with user input?

  4. What is the difference between sep and end in print()?

  5. What is an f-string in Python?

  6. Write a program that accepts two numbers and displays their product.

  7. Write a program that asks for a user's name and age, then prints a formatted message.

  8. Format a decimal number to display exactly two digits after the decimal point.

Conclusion

Python input and output functions help programs communicate with users. By learning input(), print(), type conversion, and output formatting, you can build interactive programs that accept information, perform calculations, and display clear results.

Practise the examples in your Python environment to strengthen your programming fundamentals and prepare for more advanced Python topics.

←
Post 4 of 20
→