Python basic

The input() function

The input() function in Python is used to take user input from the console. It allows you to interact with the user by prompting them to enter data, which can then be processed in your program.

Basic usage

The input() function pauses program execution and waits for the user to type something in the console. Once the user presses Enter, the function returns the input as a string.

user_input = input("Enter a value: ")

- The string passed as an argument to input() is the prompt that appears to the user.
- The returned value is always of type string, regardless of what the user enters.

Example:
name = input("What is your name? ")
print(f"Hello, {name}!")

Output:
What is your name? Alice
Hello, Alice!

In this example:
- The program prompts the user for their name.
- After entering their name, it greets them using the input.


This function is fundamental in creating interactive programs that respond to user input, making your applications more dynamic and engaging.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is the purpose of the input() function in Python?

To create a function
To take user input from the console
To print output to the console
To declare a variable
Check Answer

What type does input() return?

Integer
Float
String
Boolean
Check Answer

What will the following code output: name = input("What is your name? "); print(f"Hello, {name}!")

None
"Hello, name!"
"What is your name?"
"Hello, {name}!"
Check Answer

What happens when input() is called?

The program waits for user input
The program terminates
The program skips to the next line
The program generates a random number
Check Answer

How can input() make a program interactive?

By displaying messages to the user
By calculating results internally
By running automatically without user input
By allowing users to provide data for processing
Check Answer