Python input() Method

Oct. 17, 2022


0
2 min read
396

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.

>>> user_input = input()
How are you?
>>> user_input
'How are you?'

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

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

>>> name = input('Enter Your Name: ')
Enter Your Name: Abdulla
>>> name
'Abdulla'

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 the user what to do. Users can then enter the name after the prompt string in the same line and that would be assigned to the name variable.

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

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

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

>>> uc = input("Enter Unicode Char: ")
Enter Unicode Char: åê
>>> uc
åê
Python Method Input() Function Appreciate you stopping by my post! 😊

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required
Author's profile
Profile Image

Abdulla Fajal

Django Developer

With 'espere.in' under my care, I, Abdulla Fajal, graciously invite your insights and suggestions, as we endeavour to craft an exquisite online experience together.