Python Input and Output

Python Input and Output Functions

Python provides two very important built-in functions for interacting with users: input() and print(). These functions help take input from the keyboard and display output on the screen.


1️⃣ input() Function

The input() function is used to take input from the keyboard. It always returns a value in string format. A message can also be displayed inside the input function.

✔ Syntax

var = input("Enter something:")

Example:

  • Integer input:
a = int(input("Enter number: "))
  • Float input:
b = float(input("Enter float value: "))
  • String input:
c = input("Enter string value: ")

To convert the input data into other datatypes, Python provides:

  • int() → Converts numeric string to integer
  • float() → Converts numeric string to float
  • str() → Converts data into a string (not needed inside input())

✔ Example of type conversion

x = int(input("Enter integer: "))
y = float(input("Enter float: "))
z = str(input("Enter text: "))

2️⃣ print() Function

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

✔ Print single and multiple statements

print("Hello")                                        # Single statement
print("Hello", 12, "Hi", "Welcome")                   # Multiple parameters

✔ Print using format specifiers

Python allows C-style format specifiers: %d → integer, %f → float, %s → string

a = 12.345
b = 23.56

print("a=%d and b=%d" % (100, 200))
print("a=%f and b=%f" % (a, b))
print("a=%.2f and b=%.2f" % (a, b))   # Rounded to 2 decimals

✔ Print using placeholder format()

print("a={0} and b={1}".format(a, b))

✔ Print using String concatenation

Note: All values must be strings for concatenation.

print("a=" + str(a))
print("a=" + str(a) + " b=" + str(b))

🔹 Basic Examples of print()

print("statement1")                        # Single statement
print("statement1", "statement2")          # Multiple parameters
print("statement1" + "statement2")         # Concatenation
print("statement1" + str(10))              # int converted to string
print("result is ", 12.3444567891)         # Print float
print("result is %.2f" % 12.3444567891)    # Print with 2 decimals
print("{0},{1}".format(2,3))               # Using placeholders

✔ Example of str() conversion

s = "hello"
y = 10
print(s + str(y))

🔹 More Examples Using print()

a = 12.345
b = 23.56

print("a=%d and b=%d" % (a, b))
print("a=%f and b=%f" % (a, b))
print("a=%.2f and b=%.2f" % (a, b))
print("a=", a, "b=", b)
print(a, b)
print("a={0} and b={1}".format(a, b))
print(a + b)
print(a + b, a * b, a / b, a % b)
print("a=" + str(a) + " b=" + str(b))

📚 PROGRAM ASSIGNMENTS

  1. WAP to calculate the salary of an employee (basic, ta, da, comm, pf, no. of leave entered by the user).
  2. WAP to calculate compound interest.
  3. WAP to reverse a five-digit number where the first and last digits remain in the same position.
  4. WAP to swap two numbers without using a third variable.
  5. WAP to calculate the square and cube of any entered number.