Table of Content
- What is User Input
- One Line of String
- Input and Output
- Input Two Words Followed by an Integer
- Read Only One Word
- One Integer Per Line
- Multiple Integers on a Line
- Floats Instead of Integers
In this article at OpenGenus, we will be explaining different ways to take input in Python and also show code samples. The different methods of taking user input that I will be talking about are the use of the input function on it's own in a variable and also using print a statement to display the user input back to the user once they press enter. Other methods which I will discuss include taking in integer values as user input and taking in multiple integers on one line as user input. Also I will talking about how to have two words as an input followed by an integer and also how to read only one word even if there is more than one input value. Finally, I will discuss taking in floating point values as user input.
What is User Input
User input allows us to build interactive programs. Every programming language has its own unique implementation of an input. In most cases input stems from the keyboard, other forms like mouse clicks and track-pad etc are also possible. Python provides a simple framework for getting user input in the form of the input() function. The input function reads a line from the console and converts it into a string and returns it.
One Line of String
userInput = input()
When the program is running the user will type input for example:
User presses enter and program will finish execution.
One way to take input in python is by simply creating a variable and call the built-in function input() without passing any arguments to read the entire line as a string as shown above. What this means is that when the user runs the program they will be able to type whatever they want and that information will be stored in the
userInput variable. In order to run the program you can simply type the name of your python file in the terminal and press enter. For example if my python file was called user.py then I would type
user.py in the terminal, press enter and it will run my program.
Input and Output:
entered_value = input('Enter some value: ') print(entered_value)
When the program is running the user will be asked to type a value for example:
Enter some value: hello
When they press enter the same value will be printed:
The program will now finish execution.
In this example you can see the most common way to take an input which is done by firstly calling the input function in python. When the input() function is called, the program waits until the user types something. After the user adds some information and presses the enter key the input function returns to the program with the entered string and that is what we see happening in this example. The variable
entered_value is calling the input function with some text asking the user to type in a value and once the user does that and presses enter, that value will then be displayed back to the user because of the print statement
print(entered_value) so for example if the user types in "hello world" into the program then once they have pressed enter the program will output that exact same input
Input Two Words Followed by an Integer
words = input("Enter two words followed by an integer: ").split() word1, word2, number = words, words, int(words) print("First word:", word1) print("Second word:", word2) print("Integer:", number)
Enter two words followed by an integer: word tree 9
First word: word Second word: tree Integer: 9
In our code sample the input() function is used to prompt the user to enter a string value. The string prompt "Enter two words followed by an integer: " is displayed to the user. The user's input is obtained and assigned to the variable words.
The split() method is called on the words string to split it into a list of individual words, using whitespace as the separator. Each word becomes an element in the list
word1, word2, number = words, words, int(words). This line unpacks the elements of the words list into separate variables such as word1, word2, and number. words refers to the first word entered by the user, which is assigned to word1. words refers to the second word entered by the user, which is assigned to word2. int(words) converts the third word entered by the user into an actual integer and assigns it to a number.
print("First word:", word1)
print("Second word:", word2)
Finally, these lines print the following strings with the value of the variable assigned to them.
Read Only One Word
words = input("Enter words separated by spaces: ").split() word1 = words print("First word:", word1)
Enter words separated by spaces: cat dog shoe
First word: cat
In our code example the input() function is used to prompt the user to enter a string value. The string prompt "Enter words separated by spaces: " is displayed to the user.
The user's input is obtained and assigned to the variable words. The split() method is called on the words string to split it into a list of individual words, using whitespace as the separator. Each word becomes an element in the list.
word1 = words This line assigns the first word from the words list to the variable word1. Since list indices start from 0, words refers to the the first word in the words list.
print("First word:", word1) This line prints the string "First word:" followed by the value of the variable word1.
So, when the code is executed, it prompts the user to enter words separated by spaces. It then splits the input into individual words and assigns the first word to the variable word1. Finally, it prints the value of word1 along with the label "First word:".
One Integer Per Line
A = int(input()) B = int(input())
Example Input Values of A and B:
The input() is converted into a string but it can also be converted into integers by using
int. This will allow the user to type integer values and have them preform more complex operations like calculations. For example if your input values looks like this:
And you want to print the sum of these two integer then you can do this:
A = int(input()) B = int(input()) print(A+B)
Here as you can see we are able to do this becasue of the
int method in python. The code here is showing us two variables which are calling the int method as well as the input function. By default the input function takes values in as strings so in order to convert them to integers we call the
int method which allows us to perform the calculation shown in the print statement
print(A+B) and that is how we then get the output of 40.
Multiple Integers on a Line
Here we are going to discuss how can you take input where there are two or more integers on the same line. This will be our example:
26 14 19
In order to achieve this you can call input() to read the entire line and then call split() on the string to separate each number into a separate string and then convert each string into an integer like here:
S = input() # S => "26 14 19" Sarray = S.split() # Sarray => ["26", "14", "19"] A = int(Sarray) # A => 26 B = int(Sarray) # B => 14 C = int(Sarray) # C => 19
If you only want the program to run user input for two numbers here is what you do:
A, B = map(int, input().split())
Here the code is basically using the map method to map two variables for user input, A and B.
For three numbers:
A, B, C = map(int, input().split())
Floats Instead of Integers
userInputFloat = float(input()) print(userInputFloat)
Our example user input:
Once the user presses enter the program will then print out the users input like here:
The program will now finish execution.
If you want to read floating point numbers instead of integers from your input then use the float() function instead of int() or float instead of int when using with map(). In this code example we have created a variable
userInputFloat and called the
float() function inside of it and then we have also called the
input() function. By using the float() function we can now take in floating point values as user input such as
3.2 and then have it output back to the user as
3.2 because of our print statement