How to take two inputs in one line in Python 3

Zoey 20 Published: 08/16/2024

How to take two inputs in one line in Python 3

I'd be happy to help!

In Python 3, you can use the built-in input() function to get input from the user. By default, this function takes a single input. However, if you want to take two or more inputs in one line, you can use the following methods:

String Concatenation: You can concatenate multiple input() calls using the + operator. Here's an example:
   name = input("Enter your name: ") + " "

age = int(input("Enter your age: "))

This will first prompt the user for their name, and then ask for their age. Note that we're converting the age to an integer using int() because we want a numerical value.

Single Line Input with Spaces: You can use spaces as separators when getting input from the user. The split() method is used to split the input into separate variables:
   name_age = input("Enter your name and age (separated by space): ").split()

name = name_age[0]

age = int(name_age[1])

In this example, we're asking the user to enter their name and age separated by a space. The split() method splits the input into a list where each element is a separate variable. We then assign these elements to our variables.

Using the sys Module: You can use the sys module's argv attribute, which contains the command-line arguments passed to your program:
   import sys

if len(sys.argv) > 1:

name = sys.argv[1]

age = int(sys.argv[2])

In this example, we're assuming that the user passes their name and age as separate command-line arguments when running our program.

Using Regular Expressions: You can use the re module's search() function to extract specific patterns from the input:
   import re

pattern = r"(.*) (d+)"

match = re.search(pattern, input("Enter your name and age (separated by space): "))

if match:

name = match.group(1)

age = int(match.group(2))

In this example, we're using a regular expression to capture the name and age from the input. The search() function returns a match object if there's a match; otherwise, it returns None. We then use the group() method to extract the captured groups.

These are just some of the ways you can take two or more inputs in one line in Python 3. The choice of method depends on your specific requirements and preferences!

input().split() in python

I'd be happy to help with that!

input() is a built-in function in Python that allows you to get user input from the console. When used without any arguments, it will prompt the user for input and return what they type as a string.

Now, when it comes to split(), this is a method of strings in Python that takes an argument (which can be a character, a string, or even a regular expression) and returns a list of substrings separated by that argument. For example, if you have the string "hello,world,python", calling split(",") on it would return the list ["hello", "world", "python"].

So, when we combine these two functions together, we can use input() to get user input and then split that input into a list of substrings using split(). This is often referred to as "tokenizing" the input.

Here's an example:

user_input = input("Enter some text: ")

words = user_input.split()

print(words)

In this example, we first prompt the user for some text using input(). We then split that text into individual words using split(), which by default separates words by whitespace characters (spaces, tabs, etc.). Finally, we print out the list of words.

For instance, if the user types "Hello world!", our program would output ["Hello", "world!", "] because split() separated the text into individual words using whitespace as the delimiter.

We can also customize the behavior of split() by passing in a different delimiter. For example:

user_input = input("Enter some text: ")

comma_separated_words = user_input.split(",")

print(comma_separated_words)

In this case, if the user types "hello,world,python", our program would output [ "hello", "world", "python"] because split() separated the text into individual words using commas as the delimiter.

In summary, input() and split() are two powerful functions in Python that can be combined to tokenize user input and extract specific data from it. I hope this helps!