Python input/output

In this tutorial, you will learn about Python input/output.

Python input: A program interacts with the user or external resources like files.

Python output: It includes receiving data (input) from users, files, or other sources and displaying data on the screen.

1. Input in Python:

The input() function allows that you will get data entered by the user from the keyboard.

The data entered is always returned as a string, so you might need to convert it to other types, such as int or float.

Example:


firstName = input("Enter your first name: ")  # Takes user input as a string
print("Hello, " + firstName )            # Output: Hello, 

Converting Input Data: If the user enters a number, you can convert it to an integer or float:


age = int(input("Enter your age: "))  # Convert input to an integer
print("You are", age, "years old.")

2. Output in Python:

In Python, you will use the print() function to display output to the console. You can display strings, variables, and even formatted text.

Example


print("Hello, Friends!")  # Output: Hello, Friends!

Using Variables:


name = "John"
age = 35
print("Name:", name, "Age:", age)  # Output: Name: John Age: 35

Formatted Strings: You can use formatted strings (f-strings) to create more complex output.


name = "John"
age = 35
print(f"My name is {name} and I am {age} years old.")
# Output: My name is John and I am 35 years old.

Python input/output – Interview Questions

Q 1: How do you take input from the user in Python?

Ans: Using the input() function.

Q 2: Which function is used to display output?

Ans: The print() function is used to display output.

Q 3: What type of data does input() return?

Ans: The input() function always returns data as a string.

Q 4: How can input be converted into a number?

Ans: By using type conversion functions like int() or float().

Q 5: Can multiple values be printed at once?

Ans: Yes, multiple values can be printed using commas in print().

Python input/output – Objective Questions (MCQs)

Q1. Which function is used to take input from the user in Python?






Q2. What will be the output of the following code if the user enters 5?

x = input("Enter a number: ")
print(x * 2)






Q3. How do you convert user input into an integer in Python?






Q4. Which of the following statements correctly prints output to the screen?






Q5. What is the default end character of the print() function in Python?






Related Python input/output Topics