Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving integers from one string to another?

I am looking at adding numbers to a string as python reads through a string.

So if I had a string a = "253+"

I would then have an empty string b.

So, how would I have python read the 2, add it to string b, then read the 5 and add it to string b, and then add the 5 and add it to string b, when it hits something that isnt an integer though, it stops the function.

then string b would be b = "253"

is there a specific call in an iteration that would ask for integers and then add i to another string?

tl;dr

I want to use an iteration to add numbers from one string to another, which stops when it reaches a non-integer.

string b would be an empty string, and string a would be a="253+"

after the call would be done, strng b would equal b="253"

I know this sounds like a homework question, but its not. If you need anything else clarified, I would be happy to.

like image 980
Pewkie Avatar asked Mar 29 '26 23:03

Pewkie


1 Answers

Here is a simple method for extracting the digits from a string:

In [13]: a="253+"

In [14]: ''.join(c for c in a if c.isdigit())
Out[14]: '253'
like image 155
John1024 Avatar answered Apr 02 '26 03:04

John1024



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!