Python 3 – input() function
Last Updated :
17 Nov, 2023
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
string = input ()
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
name = input ( "Enter your name" )
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
num = int ( input ( "Enter a number:" ))
add = num + 1
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
num = float ( input ( "Enter number " ))
add = num + 1
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
li = list ( input ( "Enter number " ))
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
num = tuple ( input ( "Enter number " ))
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
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial - Python is one of the most popular programming languages today, known for its simplicity, extensive features and library support. Its clean and straightforward syntax makes it beginner-friendly, while its powerful libraries and frameworks makes it perfect for developers. Python is:
9 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Printing Output using print() in PythonAt its co
7 min read
Python basic I/O Techniques
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 ()
3 min read
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.
4 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
2 min read
Python - Print Output using print() function
Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations. [GFGTABS] Python # print() function example print("GeeksforGeeks") a = [1, 2, 'gfg'] pri
4 min read
Python - Output Formatting
In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach. Formatting Ou
6 min read
Different Input and Output Techniques in Python3
An article describing basic Input and output techniques that we use while coding in python. Input Techniques 1. Taking input using input() function -> this function by default takes string as input. Example: C/C++ Code #For string str = input() # For integers n = int(input()) # For floating or de
3 min read