Python’s built-in input()
function reads a string from the standard input. The function blocks until such input becomes available and the user hits ENTER. You can add an optional prompt
string as an argument to print a custom string to the standard output without a trailing newline character to tell the user that your program expects their input.
Here’s a minimal example of how the input()
function can be used without argument to capture the user input:
>>> s = input() 42 >>> s '42'
The user input is now stored in the variable s for further processing.
You can also ask the user for input so that they know that the program waits for them to type anything in:
>>> x = input('your input:') your input:42 >>> x '42'
🌍 Recommended Tutorial: How to Create a Python Variable At Runtime Based on a User String Input?
Python input() Video
Python input() Syntax and Examples
The Python input()
method reads a line of text from standard input and returns it as a string. It can also be used to read user input from the keyboard and store it in a variable, or to prompt the user for input so you can ask questions and get answers.
Syntax: input(prompt=None)
Argument | prompt | Optional. A string that is printed to the shell without trailing newline. This is often used to ask the user for input. |
Return Value | string | Input read from the standard input. |
Another simple example:
name = input("What's your name? ") print("Hello, " + name + "!")
Python input() vs raw_input()
The key differences between raw_input()
and input()
functions are the following:
- You can use
raw_input()
only in Python 2.x. It’s obsolete in Python 3.x and above and has been renamedinput()
- In Python 2.x,
raw_input()
returns a string whereasinput()
returns result of an evaluation. Whereas in Python 3.xinput()
returns a string but can be converted to another type like a number.
Python 2.x | Python 3.x | |
raw_input() | ◆ raw_input() accepts input as it is, i.e. exactly as the input has been entered by the user and returns a string.◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct. | ◆ raw_input() is obsolete and no longer a part of Python 3.x and above. |
input() | ◆ input() accepts the input from the user as a statement or expression and returns the output after evaluating the input. In other words, it accepts the user entry as raw_input(), performs an eval() on it, and then returns the result as output.◆ It expects a syntactically correct input (statement/expression) from the user. | ◆ In Python 3.x, raw_input() has been replaced by input() . This means that the input() function performs the same operation in Python 3.x as raw_input() used to do in Python 2. Thus input() accepts and returns a string in Python 3.x and above. |
Learn more about the differences of Python’s input()
function and the raw_input()
function in our blog tutorial:
🌍 Recommended Tutorial: Python raw_input()
vs input()
Summary
Python’s built-in input()
function reads a string from the standard input.
The function blocks until such input becomes available.
>>> s = input() 42 >>> s '42'
You can add an optional prompt
string as an argument to print a custom string to the standard output without a trailing newline character to tell the user that your program expects their input.
>>> x = input('your input:') your input:42 >>> x '42'
Want to keep improving your Python skills? Check out our free Python cheat sheets:
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.