Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python input() Function

  • Difficulty Level : Medium
  • Last Updated : 05 Nov, 2021

Python input() function is used to take user input. By default, it returns the user input in form of a string.

Syntax

input(prompt)

Parameter:

  • Prompt:  A String, representing a default message (usually screen) before the input. However, it is optional to have a prompt message.

Return: input() returns a string object. Even if the inputted value is an integer it converts it into a string.

Example 1: Taking the Name and Age of the user as input and printing it

By Default, input returns a string. So the name and age will be stored as strings.

Python




# Taking name of the user as input 
# and storing it name variable
name = input("Please Enter Your Name: ")
  
# taking age of the user as input and 
# storing in into variable age
age = input("Please Enter Your Age: ")
  
# printing it
print("The name of the user is {0} and his/her age is {1}".format(name, age))


Output:

Example 2: Taking two integers from users and adding them.

In this example, we will be looking at how to take integer input from users. To take integer input we will be using int() along with input()

Python




# Taking number 1 from user as int
num1 = int(input("Please Enter First Number: "))
  
# Taking number 2 from user as int
num2 = int(input("Please Enter Second Number: "))
  
# adding num1 and num2 and storing them in
# variable addition
addition = num1 + num2
  
# printing
print("The sum of the two given numbers is {} ".format(addition))


Output:

Similarly, we can use float() to take two float numbers. Let’s see one more example of how to take lists as input

Example 3: Taking Two lists as input and appending them

Python




# Taking list1 input from user as list
list1 = list(input("Please Enter Elements of list1: "))
  
# Taking list2 input from user as list
list2 = list(input("Please Enter Elements of list2: "))
  
  
# appending list2 into list1 using .append function
for i in list2:
    list1.append(i)
  
# printing list1
print(list1)


Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!