I've used this method before but can't find it in any of my codes so here I am on Stack Overflow :) What I'm trying to do is split an input into two( the user is asked to enter two digits separated by space). How would you call the first digit a and the second digit b ? The code so far doesn't seem to work.
a,b= input(split" "("Please enter two digits separated by space"))
Generally, user use a split () method to split a Python string but one can use it in taking multiple input. List comprehension is an elegant way to define and create list in Python.
This function helps in getting a multiple inputs from user. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, user use a split () method to split a Python string but one can use it in taking multiple input.
Definition and Usage. The split () method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
The Python standard library comes with a function for splitting strings: the split () function. This function can be used to split strings between characters. The split () function takes two parameters. The first is called the separator and it determines which character is used to split the string.
You're calling the function wrongly.
>>> "hello world".split()
['hello', 'world']
split
slits a string by a space by default, but you can change this behavior:
>>> "hello, world".split(',')
['hello', ' world']
In your case:
a,b= input("Please enter two digits separated by space").split()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With