Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python input()
In this tutorial, we will learn about the Python input() function with the help of examples.
The input() function takes input from the user and returns it.
Example
name = input("Enter your name: ")
print(name)
# Output:
# Enter your name: James
# James
input() Syntax
input('prompt')
input() Parameters
By default, the function doesn't require any parameters; however, there is one optional parameter - prompt
- prompt - text displayed before the user input
input() Return Value
The function returns the user's input in the form of a string.
Example: Python input()
# get input from user
inputString = input('Enter a string: ')
print('Input String :', inputString)
Output
Enter a string: Python is fun Input String : Python is fun
Here,
- when the user runs the program, the prompt
Enter a string:is displayed on the screen - the user enters the input value
- the entered value is stored in inputString
- the program then prints the entered value using
print
Note: Always build a habit of using prompts and make it descriptive.
More on Python input()
Taking Integer Input
We can convert an input to an integer using int(). For example,
# convert user input to integer
num = int(input("Enter a number: "))
print(f'The integer number is: {num}')
Output
Enter a number: 2 The integer number is: 2
Taking Float Input
We can convert an input to a floating point using float(). For example,
# convert user input to float
float_num = float(input("Enter a floating number: "))
print(f'The floating number is: {float_num}')
Output
Enter a floating number: 2.4 The floating number is: 2.4
Note: To learn more about int() and float() conversion, visit explicit conversion.
Did you find this article helpful?