Python 3 – input() Function

Introduction

In Python, we use the input() function to take input from the user. Whatever you enter as input, this function converts it into a string. Even if you enter an integer value, the input() function will still treat it as a string.

Python input() Function Syntax

Syntax: input(prompt)

Parameter:

  • prompt: (optional) A string that is written to standard output (usually the screen) without a newline.

Return: String object

How input() Function Works in Python

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

name = input("What is your favorite color? ")
print("Your favorite color is " + name + "!")
        

Output:

What is your favorite color? Blue
Your favorite color is Blue!

input() Function in Python Examples

Taking Input in Python

In this example, we use the input() function to take input from the user and print it.

# Taking input from the user
user_input = input("Enter something: ")
 
# Output
print(user_input)
        

Output:

Enter something: Hello World
Hello World

User Input with a Prompt

In this example, we take input from the user with a prompt and print it.

username = input("Please enter your username: ")
 
# Output
print("Welcome, " + username + "!")
        

Output:

Please enter your username: user123
Welcome, user123!

Convert User Input to a Number

In this example, we take input from the user as a string, convert it into an integer, add 1 to it, and print the result.

num = int(input("Enter a number: "))
result = num + 1
 
# Output
print(result)
        

Output:

Enter a number: 10
11

Take Float Input in Python

In this example, we take input from the user as a string, convert it into a float, add 1 to it, and print the result.

num = float(input("Enter a decimal number: "))
result = num + 1
 
# Output
print(result)
        

Output:

Enter a decimal number: 5.5
6.5

Python Accept List as Input From User

In this example, we take input from the user and convert it into a list.

user_list = list(input("Enter numbers separated by space: "))
 
# Output
print(user_list)
        

Output:

Enter numbers separated by space: 12345
['1', '2', '3', '4', '5']

Take User Input for Tuples

In this example, we take input from the user and convert it into a tuple.

user_tuple = tuple(input("Enter numbers separated by space: "))
 
# Output
print(user_tuple)
        

Output:

Enter numbers separated by space: 456
('4', '5', '6')

Input with a Dictionary Comprehension

In this example, we take space-separated words from the user and create a dictionary where each word is a key and its length is the value.

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

Output:

Enter a list of words, separated by spaces: apple banana cherry
{'apple': 5, 'banana': 6, 'cherry': 6}

Conclusion

The input() function is a fundamental aspect of Python that allows developers to interact with users. By understanding how to effectively take user input in various forms, you can enhance the functionality and user experience of your Python applications.