Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

extract numbers from string python

# Python program to extract digits from string

# take string
string = "kn4ow5pro8am2"

# print original string
print("The original string:", string)

# using join() + filter() + isdigit()
num = ''.join(filter(lambda i: i.isdigit(), string))

# print extract digits
print("Extract Digits:", num)
Comment

How to extract numbers from a string in Python?

>>> import re
>>> re.findall(r'd+', "hello 42 I'm a 32 string 30")
['42', '32', '30']
Comment

python extract all numbers from string re

>>> str = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in str.split() if s.isdigit()]
[23, 11, 2]
Comment

PREVIOUS NEXT
Code Example
Python :: access last element of list python 
Python :: pandas groupby without reset index 
Python :: python pandas remove punctuation 
Python :: auto-py-to-exe with python3 
Python :: default style matplotlib python 
Python :: chiffre cesar python 
Python :: python run another python script 
Python :: upgrade to latest django version 
Python :: bs4 find element by id 
Python :: python die 
Python :: how to print a line letter by letter in python 
Python :: tag for deleting a list in python 
Python :: what is the tracing output of the code below x=10 y=50 if(x**2 100 and y <100): print(x,y) 
Python :: python scatterplot figsize 
Python :: python get script path 
Python :: pandas read excel 
Python :: count missing values groupby 
Python :: pandas sample seed 
Python :: Jupyter notebook: let a user inputs a drawing 
Python :: python encrypt password 
Python :: pandas select row by index 
Python :: random choice dictionary python 
Python :: print without changing line python 
Python :: matplotlib title chopped off 
Python :: waitkey in opencv 
Python :: panda read data file 
Python :: Print a nested list line by line in python 
Python :: list to set keep order python 
Python :: flask define template folder 
Python :: messages django 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =