Python input() Function

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.

Python input() Gif Interactive Explanation

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'

Python input() Video

Python input() Syntax and Examples

ArgumentpromptOptional. A string that is printed to the shell without trailing newline. This is often used to ask the user for input.
Return ValuestringInput read from the standard input.
Python input() Function - Visual Explanation

Python input() vs raw_input()

The key differences between raw_input() and input() functions are the following:

  • raw_input() can be used only in Python 2.x and is obsolete in Python 3.x and above and has been renamed input()
  • In Python 2.x, raw_input() returns a string whereas input() returns result of an evaluation. While in Python 3.x input() returns a string but can be converted to another type like a number.
Python 2.xPython 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 byinput(). 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:

[Full 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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!