Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanning two integers separated by an unknown character

Tags:

python

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

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

Is there something similar I can do in Python?

like image 873
Nikhil Garg Avatar asked Dec 19 '25 20:12

Nikhil Garg


1 Answers

I'd use re.split() for this:

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

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

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

(In both cases, apply int() to the strings to convert them to integers.)

like image 61
NPE Avatar answered Dec 22 '25 10:12

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!