Scanning two integers separated by an unknown character

I want to scan two integers in python that are separated by a character (any character, not just a space).

In C, I can just use scanf("%d%c%d",&a,&b,&c);

Is there something like this in Python?

+3


source to share


2 answers


I would use re.split()

for this:

In [9]: re.split(r'\D', '1024x768')
Out[9]: ['1024', '768']

      

or, if you also need to capture the separator character:



In [11]: re.split(r'(\D)', '1024x768')
Out[11]: ['1024', 'x', '768']

      

(In both cases, apply int()

to strings to convert them to integers.)

+4


source


There is no such function in Python. You can get the String as it is and check if it is valid with regex or split

or any other function.



+1


source







All Articles