How to Take Two Inputs in One Line in Python

Python is a universally loved programming language due to its readability and versatility. Amongst its many excellent features is the ability to take multiple inputs in a single line. This tutorial will guide you to learn how to take two inputs in one line in Python 3.

Step 1: Using input() Function

In Python, the built-in input() function can be used to take standard user inputs. This function allows the user to type in some value and hit the return key to signify the end of input.

Step 2: Utilizing Python’s split() Function

To take two inputs in one line, we can use Python’s split() function in conjunction with the input() function. The split function separates the input based on a separator (space by default) and returns as list elements.

Step 3: Implementing in Python Code

Here is an example of code that uses input() and split() functions for taking two inputs in one line.

Step 4: Understanding Output

When running this code, Python will ask for two inputs separated by a space. After entering the inputs, the code will print out the two inputs you have entered.

Enter two numbers separated by a space: 5 10
The two entered values are: 5 10

Step 5: Understanding Limitations

Note that while this is a convenient method, it assumes that the user will provide exactly two inputs, separated by space. If not, the program will likely run into an error. Therefore, it’s important to ensure this input method’s usability is appropriate for your specific program.

The Full Python Code

Conclusion

This tutorial showcases how Python 3 provides a convenient way to input two values in one line using the input() and split() functions. However, care must be taken when opting for this style of input, as it may not always be suitable for every program.