close menu
Input-and-Output-functions Input and Output functions

Input and Output functions

13 February 2025

 Input and Output functions


Input and Output functions

Python provides built-in funtions for input(user data collection) and output(displaying data)operations.

1.Output Funtion: print()
The print() funtion is used to display information on the screen.

Basic Usage
print("Hello, World!")  

Output:
Hello, World!

*Printing Multiple Values
you can print multiple values sepatated by a comma(<), which automatically inserts a space.

name="Alice"
age=25
print("Name:", name, "Age:", age)

Output:
Name: Alice Age:25

*Using sep and end PArameters
•sep: Specifies the separator between multiple values.
•end: Specifieswhat should be printed at the end(default is newline\n).

print("python", "fun", sep="-")
print("Hello", end=" ")
print("world")

Output:
python-is-fun
Hello World

2.Input Function: input()
The input() funtion is used to take user input as a string.

Basic Input
name=input("Enter your name:")
print("Hello", name)

Example Interaction:
Enter your name: Alice
Hello, Alice

Converting Input Data Type
By default, input() returns a string. To take numbers as input, you need to convert them.

age=int(input("Enter your age:"))
print("You will be", age+1, "next year!")

Example Interaction:
Enter your age: 25
You will be 26 next year!

*talking Multiple Inputs
To take multiple inputs in a single line, you can use split().

name, age = input("Enter youor name and age:").split()
print("Name:", name, "Age:" age)

Example Interaction:
Enter your name and age: Alice 25
Name: Alice Age: 25

You can also specify a separator:
x, y = input("Enter two numbers separated by a comma: ").split(",")
print("First number:", x, "Second number:", y)


3.Formatting Output(f-strings)
Using f-strings (Python 3.6+), you can format output effciently.

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

Output:
My name is Alice and I am 25 years old.
 

Whatsapp logo