Python input() Method
input() method reads the user's input and returns it as a string.
prompt: (Optional) A string, representing the default message before taking input.
The following example takes the user input using the
>>> user_input = input() How are you? >>> user_input 'How are you?'
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
So, the value of a
user_input would be whatever user has typed.
The following example demonstrets how to use the optional
>>> 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
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'>
User can enter Unicode characters as well, as shown below.
>>> uc = input("Enter Unicode Char: ") Enter Unicode Char: åê >>> uc åê