Beginner Level
Intermediate Level
Advanced Level
Introduction
Python, a high-level programming language, is widely used by programmers across the world. One of the essential features of any programming language is the ability to take input from the user. In Python, the input() function is used to accomplish this. This function can receive any input value, be it a string, integer, or a float, from the user and store it in a variable. In this tutorial, we will explore the powerful input() function in Python and learn how to take input from the user and integrate it into our code effectively. So, let's dive into the basics of taking user input in Python!
Table of Contents :
- Using the input() method in Python
- Type casting along with input() method
- Prompt parameter of input() method
Using the input() method in Python
- Python allows us to take user input.
- We can use the input() function to take input from a user.
- input() function takes whatever the user types in and stores it as a string.
- Code Sample :
print("Please enter your Id: ")
id = input()
print("Your Id is : ", id)
# Output
# Please enter your Id:
# 10
# Your Id is : 10
# Here, we first print a prompt to the user.
# In the prompt we ask the user to enter Id.
# We then use the input() function to take input from the user.
# The value entered by the user is stored in variable id.
# We can then use the id variable in our program.
Type casting along with input() method :
- If you want to take input as an integer, we can use the int() function to convert the string to an integer.
- Code Sample :
print("Please enter your age: ")
age = int(input())
next_age = age + 5
print("You will be",next_age , "years old after 5 years.")
# Output
# Please enter your age:
# 25
# You will be 30 years old after 5 years.
# Here, we first print a prompt to the user.
# In the prompt we ask the user to enter age.
# We then use the input() function to take input from the user.
# We use the int() function to convert the string to an integer.
# We can then use the age variable in our program.
- Similarly if we want to take input from the user as a floating point number, we can use the float() function to convert the string to a floating point number.
- Code Sample :
print("Please enter your weight : ")
weight = float(input())
print("You weigh", weight, "kilograms.")
# Output
# Please enter your weight :
# 65.4
# You weigh 65.4 kilograms.
# Here, we first print a prompt to the user.
# In the prompt we ask the user to enter weight.
# We then use the input() function to take input from the user.
# We use the float() function to convert the string to float.
# We can then use the weight variable in our program.
Prompt parameter of input() method :
- We can use the prompt parameter to display a message to the user.
- The prompt parameter of the input method prints a message on the screen for the user to enter some value.
- We do not have to use the print statement separately if we use the prompt parameter.
weight = float(input("Please enter your weight : "))
print("You weigh", weight, "kilograms.")
# Output
# Please enter your weight : 65.4
# You weigh 65.4 kilograms.
# Here, We use the input() function to take input from the user.
# we use the prompt parameter of the input method.
# In the prompt we ask the user to enter weight.
# We use the float() function to convert the string to float.
# We can then use the weight variable in our program.
Prev. Tutorial : Operator special functions
Next Tutorial : Displaying output