- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get integer values from a string in Python?
You can use regular expressions to get all integer values in order of their occurance in an array. You can use the following code to get those values -
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
[12, 52, 19, 15]
If you want to concatenate all numbers in one number and output that, then you can use str.isdigit method to filter them. For example,
>>> s = "12 hello 52 19 some random 15 number" >>> print int(filter(str.isdigit, s)) 12521915
- Decrypt String from Alphabet to Integer Mapping in Python
- Get unique values from a list in Python
- How to get a string from a tkinter filedialog in Python 3?
- How can we get substring from a string in Python?
- Python Pandas - Get unique values from a column
- How to get a list of all the values from a Python dictionary?
- How to convert an integer to a hexadecimal string in Python?
- How to convert an integer to a octal string in Python?
- How to convert String to Integer and Integer to String in Java?
- How to get max alphabetical character from the string in Python?
- How to get min alphabetical character from the string in Python?
- Java Program to convert from integer to String
- Java Program to convert from String to integer
- How to get parameters from a URL string in PHP?
- How to convert a string to a integer in C