Open In App

Python input() Function

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

input() Function 

Syntax: 

input(prompt)

prompt [optional]: any string value to display as input message

Ex: input(“What is your name? “)

Returns: Return a string value as input by the user.

By default input() function helps in taking user input as string.
If any user wants to take input as int or float, we just need to typecast it.

Refer to all datatypes and examples from here.

Python input() Function Example

Python3




# Taking input as string
color = input("What color is rose?: ")
print(color)
 
# Taking input as int
# Typecasting to int
n = int(input("How many roses?: "))
print(n)
 
# Taking input as float
# Typecasting to float
price = float(input("Price of each rose?: "))
print(price)


Output:

What color is rose?: red
red
How many roses?: 10
10
Price of each rose?: 15.50
15.5

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: ")
 
print("Name & Age: ", name, age)


Output:

Please Enter Your Name: Rohit
Please Enter Your Age: 16
Name & Age:  Rohit 16 

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 Python 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

Taking user input as a string and splitting on each character using list() to convert into list of characters.

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:



Previous Article
Next Article

Similar Reads

Python 3 - input() function
In Python, we use the 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 converts it into a string. Python input() Function SyntaxSyntax: input(prompt) Parameter: Prompt: (optional) The string that is written to standard output
3 min read
Vulnerability in input() function – Python 2.x
This article aims to explain and explore the vulnerability in the input() function in Python 2.x. In Python 3, the raw_input() function was erased, and its functionality was transferred to a new built-in function known as input(). Different Ways to Input Data in Python 2.xThere are two common methods to receive input in Python 2.x: input() function
5 min read
How to Compute the Heaviside Step Function for Each Element in Input in PyTorch?
In this article, we are going to cover how to compute the Heaviside step function for each element in input in PyTorch using Python. We can compute this with the help of torch.heaviside() method. torch.heaviside() method The torch.heaviside() method is used to compute the Heaviside step function for each element. This method accepts input and value
2 min read
Generate two output strings depending upon occurrence of character in input string in Python
Given an input string str[], generate two output strings. One of which consists of that character that occurs only once in the input string and the second consists of multi-time occurring characters. Output strings must be sorted. Examples: Input : str = "geeksforgeeks" Output : String with characters occurring once: "for". String with characters o
4 min read
Python | Find all close matches of input string from a list
We are given a list of pattern strings and a single input string. We need to find all possible close good enough matches of input string into list of pattern strings. Examples: Input : patterns = ['ape', 'apple', 'peach', 'puppy'], input = 'appel' Output : ['apple', 'ape'] We can solve this problem in python quickly using in built function difflib.
2 min read
Taking input from console in Python
What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks like this. Here we write a command and to execute
2 min read
Get a list as input from user in Python
We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to get input a list from the user using Python. Example: Input : n = 4, ele = 1 2 3 4Output : [1, 2, 3, 4]Input : n = 6, ele = 3 4 1 7 9 6Output : [3, 4, 1, 7, 9, 6]Get a list as input from user in Python using Loop C/C++ Cod
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Python regex | Check whether the input is Floating point number or not
Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Output: Not a Floating point number In this program, we
2 min read
Compute the square root of negative input with emath in Python
In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy. Example:Input: [-3,-4] Output: [0.+1.73205081j 0.+2.j ] Explanation: Square root of a negative input.NumPy.emath.sqrt method: The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex val
2 min read
Article Tags :
Practice Tags :