Open In App
Related Articles

Python 3 – input() function

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

Example:

Python3




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

Output :

What is your name? GFG
Hello, GFG!

Python input() Syntax

Syntax: input(prompt)

Parameter:

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

Return: String object

input() in Python Examples

Taking input in Python

In this example, we are using the Python input() function 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 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')

Using input with a dictionary comprehension

In this example, we are taking the words separated by space and we make a dictionary of the word as key with their length as 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


Last Updated : 31 Jul, 2023
Like Article
Save Article
Similar Reads
Related Tutorials