Python input() built-in function

From the Python 3 documentation

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

Introduction

The input() function in Python is a built-in function that allows a program to read a line of text from the user’s keyboard. It is an essential tool for creating interactive applications, as it pauses the program’s execution and waits for the user to provide input. The function can also display a prompt to guide the user on what to enter.

Examples

This function takes the input from the user and converts it into a string:

>>> print('What is your name?')   # ask for their name
>>> my_name = input()
>>> print('Hi, {}'.format(my_name))
# What is your name?
# Martha
# Hi, Martha

input() can also set a default message without using print():

>>> my_name = input('What is your name? ')  # default message
>>> print('Hi, {}'.format(my_name))
# What is your name? Martha
# Hi, Martha

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.