Python 3 – input() function
In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into a string.
Syntax: input(prompt)
Parameter:
- Prompt: (optional) The string that is written to standard output(usually screen) without newline.
Return: String object
Let’s see the examples:
Example 1: Taking input from the user.
- Python3
# Taking input from the user string = input () # Output print (string) |
Output:
geeksforgeeks
Example 2: Taking input from the user with a message.
- Python
# Taking input from the user name = input ( "Enter your name" ) # Output print ( "Hello" , name) |
Output:
Enter your name:ankit rai Hello ankit rai
Example 3: By default input() function takes the user’s input in a string. So, to take the input in the form of int you need to use int() along with the input function.
- Python3
# Taking input from the user as integer num = int ( input ( "Enter a number:" )) add = num + 1 # Output print (add) |
Output:
Enter a number:15 16
Example 4: Let’s take float input along with the input function.
- Python3
# Taking input from the user as float num = float ( input ( "Enter number " )) add = num + 1 # output print (add) |
Output:
Enter number 5 6.0
Example 5: Let’s take list input along with the input function.
- Python3
# Taking input from the user as list li = list ( input ( "Enter number " )) # output print (li) |
Output:
Enter number 12345 ['1', '2', '3', '4', '5']
Example 6: Let’s take tuple input along with the input function.
- Python3
# Taking input from the user as tuple num = tuple ( input ( "Enter number " )) # output print (num) |
Output:
Enter number 123 ('1', '2', '3')
Last Updated on November 13, 2021 by admin