python get int from string

Do you want to know the details regarding “python get int from string”. If yes, you’re in the correct tutorial.

python get int from string

>>> import re
>>> string1 = "498results should get"
>>> int(re.search(r'\d+', string1).group())
498
string = "abc123"
# Method 1
''.join(char for char in string if char.isdigit())

#Method 2
import re
re.sub("[^0-9]", "", string)
import re
s = "12 hello 52 19 some random 15 number"
# Extract numbers and cast them to int
list_of_nums = map(int, re.findall('\d+', s))
print list_of_nums

Conclusion

I hope this tutorial helps you to know about “python get int from string”. If you have any questions regarding this tutorial please let us know via the comment section. Share this tutorial with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment