In Python (programming language), input and output allow your program to interact with the user. You can receive data from the user and display information on the screen.
π Getting Input from the User
Python provides the input() function to take input from the user. The input is always returned as a string.
Example:
name = input("Enter your name: ")
print("Hello, " + name + "!")
Sample Run:
Enter your name: Rahul
Hello, Rahul!
π Input Type Conversion
Since input() returns a string, you often need to convert it to a number using type casting:
age = int(input("Enter your age: "))
print("Next year, you will be", age + 1)
π¨ Displaying Output
Python uses the print() function to display output on the screen.
Examples:
# Printing text
print("Hello, World!")# Printing variables
name = "Alice"
print("Name:", name)# Printing multiple values
age = 25
print(name, "is", age, "years old")
Output:
Hello, World!
Name: Alice
Alice is 25 years old
β¨ Formatting Output
Python provides ways to format output nicely.
1οΈβ£ Using + for concatenation
name = "Alice"
age = 25
print("Name: " + name + ", Age: " + str(age))
2οΈβ£ Using Commas (Automatic space)
print("Name:", name, "Age:", age)
3οΈβ£ Using f-strings (Python 3.6+)
print(f"{name} is {age} years old")
π’ Input Multiple Values
You can take multiple inputs using split():
x, y = input("Enter two numbers separated by space: ").split()
x = int(x)
y = int(y)
print("Sum:", x + y)
Sample Run:
Enter two numbers separated by space: 10 20
Sum: 30
β Key Points
input(): Takes input from the user (always string).print(): Displays output on the screen.- Use type casting to convert input to
int,float, etc. - f-strings are the easiest way to format output.