What is the purpose of the input() function in Python?
a) To display output on the screen
b) To read a line of text from the user
c) To calculate the length of a string
d) To convert data types
Answer:
b) To read a line of text from the user
Explanation:
The input()
function in Python is used to read a line of text entered by the user. The function waits for the user to type some input and press Enter, then returns the input as a string.
name = input("Enter your name: ")
print(f"Hello, {name}!") # Greets the user with their inputted name
The input()
function is commonly used in programs that require user interaction, such as command-line tools, games, and interactive scripts.
Understanding how to use the input()
function is essential for building applications that interact with users by collecting and processing their input.