The Wayback Machine - https://web.archive.org/web/20240827103158/https://www.geeksforgeeks.org/python-input-function/
Open In App

Python input() Function

Last Updated : 02 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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

Image

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:

Image

Python input() Function – FAQs

How to use the input() function in a Python script?

The input() function in Python is used to take user input. It reads a line from the input (usually from the user), converts it to a string, and returns it.

Can we provide a basic example of using the input() function?

Yes, here’s a basic example:

name = input("Enter your name: ")
print("Hello, " + name + "!")

How to store the value entered by the user using the input() function?

You can store the value entered by the user in a variable. Here’s how you can do it:

user_input = input("Please enter something: ")
print("You entered:", user_input)

How does the input() function handle different data types?

The input() function always returns the user input as a string, regardless of what the user types. If you need a different data type (like an integer or float), you have to convert it manually.

How can we convert the input received from the input() function to an integer?

You can use the int() function to convert the input to an integer. Here’s an example:

user_input = input("Enter a number: ")
number = int(user_input)
print("The number you entered is:", number)

If you need to convert to other data types, you can use float() for floating-point numbers, bool() for boolean values, etc.



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
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
Python | Accepting Script Input
A lot of people use Python as a replacement for shell scripts, using it to automate common system tasks, such as manipulating files, configuring systems, and so forth. This article aims to describe accepting Script Input via Redirection, Pipes, or Input Files. Problem - To have a script to be able to accept input using whatever mechanism is easiest
2 min read
PyQt5 Input Dialog | Python
PyQt5 provides a class named QInputDialog which is used to take input from the user. In most of the application, there comes a situation where some data is required to be entered by the user and hence input dialog is needed. Input can be of type String or Text, Integer, Double and item.Used methods: These methods return tuple having two elements -
2 min read
Difference between input() and raw_input() functions in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ( prompt )raw_input ( prompt )input() function Pytho
4 min read
Take input from stdin in Python
In this article, we will read How to take input from stdin in Python. There are a number of ways in which we can take input from stdin in Python. sys.stdininput()fileinput.input()Read Input From stdin in Python using sys.stdin First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for stand
2 min read
Python VLC MediaPlayer – Enabling Keyboard Input
In this article, we will see how we can enable keyboard input handling the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediaPlayer object is the basic object in vlc module for playing the video. By
2 min read
Python VLC MediaPlayer – Enabling Mouse Input Handling
In this article we will see how we can enable mouse input handling the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediPlyer object is the basic object in vlc module for playing the video. By input
2 min read
Replace infinity with large finite numbers and fill NaN for complex input values using NumPy in Python
In this article, we will cover how to fill Nan for complex input values and infinity values with large finite numbers in Python using NumPy. Example: Input: [complex(np.nan,np.inf)] Output: [1000.+1.79769313e+308j] Explanation: Replace Nan with complex values and infinity values with large finite. numpy.nan_to_num method The numpy.nan_to_num method
3 min read
Python Arcade - Handling Keyboard Input
In this article, we will discuss how to handle keyboard inputs in Python arcade module. In Arcade, you can easily check which keyboard button is pressed and perform tasks according to that. For this, we are going to use these functions: on_key_press()on_key_release() Syntax: on_key_press(symbol,modifiers)on_key_release (symbol, modifiers) Parameter
4 min read
Using a Class with Input in Python
In this article, we will see how to take input using class in Python. Using a Class with Input in Python It is to be noted that while using class in Python, the __init__() method is mandatory to be called for declaring the class data members, without which we cannot declare the instance variable (data members) for the object of the class. A variabl
2 min read
Python NumPy - Replace NaN with zero and fill positive infinity for complex input values
In this article, we will see how to replace NaN with zero and fill positive infinity for complex input values in Python. Numpy package provides us with the numpy.nan_to_num() method to replace NaN with zero and fill positive infinity for complex input values in Python. This method substitutes a nan value with a number and replaces positive infinity
4 min read
Return the result of the power to which the negative input value is raised with scimath in Python
In this article, we will discuss how to Return the result of the power to which the negative input value is raised with scimath in Python and NumPy. Example Input: [-1,-2,-3] Output: [1.-0.j 4.-0.j 9.-0.j] Explanation: It return x to the power p, (x**p), If x contains negative values, the output is converted to the complex domain. NumPy.lib.scimath
3 min read
Return the result of the power to which the input value is raised with scimath in Python
This article will discuss how to Return the result of the power to which the input value is raised with sci math in Python using NumPy. Example Input: [1,2,3] Output: [1 4 9] Explanation: It returns x to the power p, (x**p).NumPy.lib.scimath.power method The lib.scimath.power() method from the NumPy package is used to return the power to which the
2 min read
Python NumPy - Return real parts if input is complex with all imaginary parts close to zero
In this article, we will discuss how to return real parts if the input is complex with all imaginary parts close to zero in Python. The numpy np.real_if_close() method is used to return the real parts if the input is a complex number with all imaginary parts close to zero. “Close to zero” is defined as tol * (machine epsilon of the type for a). syn
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
How to set an input time limit in Python?
In this article, we will explain how to set an input time limit in Python. It is one of the easiest programming languages which is not only dynamically typed but also garbage collected. Here we will look at different methods to set an input time limit. Below are the methods we will use in this article. Methods to Set an Input Time Limit in PythonUs
6 min read
How to Take Only a Single Character as an Input in Python
Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If the user inputs a single character, then break out
3 min read
Save user input to a Excel File in Python
In this article, we will learn how to store user input in an excel sheet using Python, What is Excel? Excel is a spreadsheet in a computer application that is designed to add, display, analyze, organize, and manipulate data arranged in rows and columns. It is the most popular application for accounting, analytics, data presentation, etc. Methods us
4 min read
Python Input Methods for Competitive Programming
Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. In online coding platforms, if the C/C++ limit provided is x. Usually, in Java time provided is 2x, and in Python, it's 5x. To improve the speed of code execution for input/output intensive problems, languages have
6 min read
fileinput.input() in Python
With the help of fileinput.input() method, we can get the file as input and can be used to update and append the data in the file by using fileinput.input() method. Python fileinput.input() Syntax Syntax : fileinput.input(files) Parameter : fileinput module in Python has input() for reading from multiple files.input() in fileinput reads input from
2 min read
How to input multiple values from user in one line in Python?
For instance, in C we can do something like this: C/C++ Code // Reads two values in one line scanf("%d %d", &x, &y) One solution is to use raw_input() two times. C/C++ Code x, y = input(), input() Another solution is to use split() C/C++ Code x, y = input().split() Note that we don't have to explicitly specify spli
2 min read
How To Get The Input From A Checkbox In Python Tkinter?
The checkbox is called Checkbutton in Tkinter. Checkbutton allows users to choose more than 1 choice under a category. For example, we need not have only one choice when ordering food from a restaurant. We can choose 2 or 3 or more foods to order from the restaurant. In those applications, we use the check button in Tkinter. How to create a Checkbo
3 min read
Taking input in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ( prompt ) raw_input ( prompt ) input (): This funct
5 min read
Article Tags :
Practice Tags :