Open In App

Python 3 – input() function

Last Updated : 17 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Syntax

Syntax: input(prompt)

Parameter:

  • Prompt: (optional) The string that is written to standard output(usually screen) without newline.

Return: String object

How input() Function works in Python?

In this example, we are using input() function to input user data as a string in Python.

Python3




name = input("What is your name? ")
print("Hello, " + name + "!")


Output

What is your name? GFG
Hello, GFG!

input() Function in Python Examples

Taking input in Python

In this example, we are using the Python input() function to input user data as a string in Python, which takes input from the user and prints it.

Python3




# Taking input from the user
string = input()
 
# Output
print(string)


 Output

geeksforgeeks

User Input in Python

In this example, we are taking input from the user and input user data as a string in Python with a prompt and printing it.

Python




# Taking input from the user
name = input("Enter your name")
 
# Output
print("Hello", name)


 Output

Enter your name:ankit rai
Hello ankit rai

Convert User Input to a Number

In this example, we are using the Python input() function which takes input from the user in string format converting it into an integer adding 1 to the integer, and printing it.

Python3




# Taking input from the user as integer
num = int(input("Enter a number:"))
add = num + 1
 
# Output
print(add)


Output

Enter a number:15
16

Take float input in Python

In this example, we are using the Python input() function which takes input from the user in string format converts it into float adds 1 to the float, and prints it.

Python3




# Taking input from the user as float
 
num =float(input("Enter number "))
add = num + 1
 
# output
print(add)


Output

Enter number 5
6.0

Python Accept List as a input From User

In this example, we are taking input from the user in string format converting it into a list, and printing it.

Python3




# Taking input from the user as list
 
li =list(input("Enter number "))
 
# output
print(li)


Output

Enter number 12345
['1', '2', '3', '4', '5']

Take User Input for Tuples and Sets

In this example, we are taking input from the user in string format converting it into a tuple, and printing it.

Python3




# Taking input from the user as tuple
 
num =tuple(input("Enter number "))
 
# output
print(num)


Output

Enter number 123
('1', '2', '3')

Input with a dictionary comprehension

In this example, we are taking the words separated by space to input user data as a string in Python, and we make a dictionary of the word as the key with their length as the value.

Python3




words_str = input("Enter a list of words, separated by spaces: ")
words = {word: len(word) for word in words_str.split()}
print(words)


Output

Enter a list of words, separated by spaces: geeks for geeks
{'geeks': 5, 'for': 3}

Also, check:

Start your Python 3 journey with our extensive guide: Python 3 Tutorial



Previous Article
Next Article

Similar Reads

Python input() Function
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 i
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 :