Python input()

Python input() builtin function is used to read a line from standard input and return the read data as a string.

In this tutorial, we will learn about the syntax of Python input() function, and learn how to use this function with the help of examples.

Syntax

The syntax of input() function is

input()

where

ParameterRequired/OptionalDescription
promptOptionalA string value.

If prompt is given to input(), then prompt string is printed to the standard output, and then waits for the user input. After the user inputs a line, input() reads the line and returns as string.

prompt() helps to ask the user what data we would need from him/her via input.

Returns

The function returns string object.

Example – input(prompt)

In this example, we will provide a prompt and read the input from user via standard input.

Python Program

prompt = 'Enter a message : '
result = input(prompt)
print(f'Return value : {result}')

Output

Enter a message :

Prompt appears in the standard output. Enter a message and click enter.

Output

Enter a message : hello world
Return value : hello world

input() – No prompt

In this example, we will not provide any prompt.

Python Program

result = input()
print(f'Return value : {result}')

Output

$ python example.py

No prompt appears in the standard output. But, when we enter a string, it is echoed character after character. After completion of entering the message, click enter key.

Output

hello world
Return value : hello world

Conclusion

In this Python Tutorial, we have learnt the syntax of Python input() builtin function, and also learned how to use this function, with the help of Python example programs.