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())
Contribute to Docs
- 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.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours