In Python, the input() function enables you to accept data from the user. The function is designed so that the input provided by the user is converted into a string.
In this brief guide, you'll learn how to use the input() function.
Syntax of the input() Function
The input() function is quite straightforward to use with this syntax:
input(prompt)
|
If you use the optional parameter, the function can accept a string written without a newline character in the standard output. It returns a string object.
Here's a simple example of the input() function in action:
IDnumber = input("Please enter your ID number: ") print("Welcome, " + IDnumber + "!") |
The output of this code is:
Please enter your ID number: 123 Welcome, 123! |
Examples of input() Function
The input() function can be used in a variety of scenarios:
#1 Accepting an Input
To take an input from the user and print it, you can write a simple Python script like this:
# Prompting user for input and storing it in a variable userInput = input() # Printing the user's input print(userInput) |
The output is:
Enter anything: print this print this |
#2 Converting the Input to a Number
As mentioned earlier, the input() function accepts inputs and converts them into strings. However, you can change the data type of the accepted data before storing it.
Here's an example of accepting a number and adding 7 to it:
# Accepting an input and typecasting to an integer number = int(input(“Type an integer: ”)) addition = number + 7 # Printing the output print(addition) |
The output of this code is:
Type an integer: 5 12 |
#3 Accepting a Float Input
Interestingly, you can typecast the data that input() accepts into floating-type data. The method of doing this is quite straightforward:
# Accepting an input and typecasting to a floating point number number = float(input(“Type a number: ”)) addition = number + 7 # Printing the output print(addition) |
Running this code will generate this output:
Type a number: 8 15.0 |
#4 Accepting a List with input()
Converting the data that the input() function receives into a list is not as complicated as it may seem. Here's an example of accepting some data and storing it as a list:
# Accepting an input and converting it into a list numberList = list(input(“Type some data: ”)) # Printing the output print(numberList) |
Here's what happens if you give this program some numbers:
Type some data: 74839 ['7', '4', '8', '3', '9'] |
#5 Accepting Sets and Tuples
In the code below, we accept a string input from the user and convert it into a tuple before printing it:
# Accepting an input and converting it into a tuple numberTuple = tuple(input(“Type some data: ”)) # Printing the output print(numberTuple) |
Here's what happens if you give this program some numbers:
Type some data: 74839 ('7', '4', '8', '3', '9') |
#6 Converting Input into a Dictionary
To turn an input into a dictionary, you must accept words separated by spaces. You can then make a dictionary of the words with their lengths as the values.
Let's see it in action:
stringVariable = input("Enter some words with spaces between them: ") words = {word: len(word) for word in stringVariable.split()} print(words) |
The output is:
Enter some words with spaces between them: John Doe {'John': 4, 'Doe': 3} |