Tutorialsteacher

Follow Us

Python input() Method

The input() method reads the user's input and returns it as a string.

input() Syntax:

input(prompt)

Parameters:

prompt: (Optional) A string, representing the default message before taking input.

Return Value:

A string.

The following example takes the user input using the input() method.

Example: Take User Input
>>> user_input = input() How are you? >>> user_input 'How are you?'

Above, the input() function takes the user's input in the next single line, so whatever user writes in a signle line would be assign to to a variable user_input. So, the value of a user_input would be whatever user has typed.

The following example demonstrets how to use the optional prompt parameter.

Example: Input with Prompt
>>> name = input('Enter Your Name: ') Enter Your Name: Steve >>> name 'Steve'

In the above example, the input('Enter Your Name: ') function with prompt string 'Enter Your Name: '. So, in the next line it will display a prompt first, asking user what to do. User can then enter the name after the prompt string in the same line and that would be assign to the name variable.

The input() function converts all the user input to a string even if that's the number.

Example: Input with Prompt
>>> age = input('Enter Your Age: ') Enter Your Name: 25 >>> age '25' >>> type(age) <class 'str'>

User can enter Unicode characters as well, as shown below.

Example: input() with Unicode Chars
>>> uc = input("Enter Unicode Char: ") Enter Unicode Char: åê >>> uc åê