Simple Input and Output | Python Tutorial - Learn with VOKS
Back Next

Simple Input and Output


Input is data that a user gives to a program.

Example:

  • Typing your name
  • Entering your age

✅ Output

Output is the result the program shows to the user.

Example:

  • Displaying "Hello Danny"
  • Showing the sum of two numbers

Taking Input in Python

Python uses the input() function.

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

What happens?

  1. Program waits for the user
  2. User types something
  3. It is stored in a variable

⚠️ Important: Input is Always a String

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

Even if you type 25, Python stores it as "25".

Converting Input to Other Data Types

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

Type Conversion Table

| Function | Converts To | Example |
|----------|------------|---------|
int() | Integer | int("5") → 5 |
float() | Decimal number | float("3.5") → 3.5 |
str() | String | str(10) → "10" |

Output in Python

Python uses the print() function.

print("Hello World")

Printing Variables

name = "Danny"
print(name)

Printing Multiple Items

name = "Danny"
age = 25

print(name, age)

Default Separator = Space

Output:

Danny 25

Custom Separator

print("Python", "Java", "C++", sep=" | ")

Output:

Python | Java | C++

Custom End Character

print("Hello", end=" ")
print("World")

Output:

Hello World

Formatting Output (Very Important)

✅ Using f-strings (Best Method)

name = "Danny"
score = 95

print(f"My name is {name} and I scored {score}")

✅ Using format()

print("My name is {} and I scored {}".format(name, score))

✅ Using % formatting (Old method)

print("My name is %s and I scored %d" % (name, score))

Output Formatting Methods

| Method | Example | Result |
|--------|---------|--------|
f-string | f"{name}" | Danny |
format() | "{}".format(name) | Danny |
% operator | "%s" % name | Danny |

Printing Special Characters

Escape Characters

| Escape | Meaning |
|--------|---------|
\n | New line |
\t | Tab space |
\" | Double quote |
\' | Single quote |
\\ | Backslash |

Example:

print("Hello\nWorld")

Output:

Hello
World

Taking Multiple Inputs

Method 1: Separately

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

Method 2: In One Line

a, b = input("Enter two numbers: ").split()

For numbers:

a, b = map(int, input("Enter two numbers: ").split())

Examples

Example 1: Simple Calculator

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

sum = num1 + num2

print("The sum is:", sum)

Example 2: Student Details

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

print(f"Student name is {name} and age is {age}")

Example 3: Temperature Converter

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9/5) + 32

print(f"Fahrenheit: {fahrenheit}")

⚠️ Common Beginner Mistakes

| Mistake | Why It Happens | Solution |
|----------|---------------|----------|
Adding numbers without int() | Input is string | Convert using int() |
Forgetting quotes in print | Text must be string | Use " " |
Wrong variable order | Logic error | Check variable placement |

Summary

| Concept | Function | Example |
|----------|-----------|----------|
User input | input() | input("Enter name: ") |
Display output | print() | print("Hello") |
Convert to int | int() | int(input()) |
Convert to float | float() | float(input()) |
Formatted output | f-string | f"{name}" |
Multiple input | split() | input().split() |

Tip

Flow of a Python Program:

INPUT → PROCESS → OUTPUT

Example:

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

result = num1 + num2

print("Answer is:", result)


Python
Intorduction Python Syntax Compare to other Programming Languages How to Install Python Print Statement Python Comments Data Structure and Data Type String Operations in Python Simple Input and Output Simple Output Formatting
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL