Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Python) How to extract numbers from a string (without regex)? [duplicate]

Tags:

python

string

I would like to extract all the numbers contained in a string. I can't use regex, is there any other way?

Example:

minput = "BLP45PP32AMPY"

Result:

4532
like image 715
BrilliantPy Avatar asked Jan 25 '23 07:01

BrilliantPy


1 Answers

You can use str.isnumeric:

minput = "BLP45PP32AMPY"

number = int("".join(ch for ch in minput if ch.isnumeric()))
print(number)

Prints:

4532
like image 79
Andrej Kesely Avatar answered Jan 26 '23 19:01

Andrej Kesely