Open In App

Python 3 – input() function

Last Updated : 17 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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



Next Article

Similar Reads

Article Tags :
Practice Tags :
three90RightbarBannerImg
  翻译: