input()

The input() built-in 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, the input is converted to and returned as a string.

Example 1

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}")

Example 2

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 difference
print(completeOne())

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy