input()
The built-in input()
function prompts the user for data that is converted to and returned as a string. This function is unique in that the environment creates a field to allow users to enter the value.
Syntax
input(prompt_string)
If a prompt_string
is given, it is printed without a trailing newline. After the user types something and presses the Enter key, a string representation of the input is returned. This function encourages flexibility, usability, and personalization of set workflow. Cases in which this is used include webpages, desktop, and console applications.
Note: The
input()
function is the primary user input function for Python 3.x while theraw_input()
function was used in Python 2.x, with support and updates discontinued since January 2020.
Examples
The following example snippet showcases how the input()
function can be used with or without arguments:
without_prompt = input()print(f"Without prompt: {without_prompt}")with_prompt = input("What is your name? ")print(f"With prompt: {with_prompt}")
Sometimes, the returned string from the input()
function needs to be converted to another data type, like in the following example with float values:
def completeOne():fraction = input("Pick a decimal number between 0 and 1: ")difference = 1 - float(fraction)return differenceprint(completeOne())
All contributors
- BrandonDusch580 total contributions
- ebubetech3 total contributions
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributorAnonymous contributor186 total contributions
- BrandonDusch
- ebubetech
- christian.dinh
- Anonymous contributor
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.