How to take multiple inputs in a single line: Python?
Hello Coders, this tutorial deals with a program to take multiple inputs in a single line in Python.
Let us see a code to take one input.
s=input('Enter a character:')
Using the above code we can take input for any variable but if you want to take input of your preferred data type then use this syntax: datatype(input(‘<statement>’))
Example:
n=int(input('Enter a number:'))
If you want to take input for more than one variable then we generally go for below one.
a=input('Enter a character:') b=input('Enter a character:')
Take multiple input with a single line in Python
We can use it in a single statement also like below.
a,b=input(),input() print(a) print(b)
Output:
Here above code is single-line but we need to give input in multiple lines.
But if we want to code in a single line and also want to give input in a single line then go for split() method.
a,b=input(Enter two characters separated by space:').split()
Output:
So in these ways, we can take multiple inputs in a single line in Python. For any queries please comment below.
Also read: Python input( ) function
How to take user defined input in Python?
Extract single and multiple rows using pandas.DataFrame.iloc in Python
Leave a Reply