Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in Python only
Search
Advanced Search
Forums
Product Launch
Updates
Today's Posts
Member List
Calendar
Home
Forum
Topic
Python
strings
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
wanstrjm
New Member
Join Date:
Sep 2007
Posts:
1
#1
strings
Sep 22 '07, 11:23 PM
How do I get the digits out of a string?
Example: string1 = "23.45a"
I need to remove the alpha characters and keep the digits.
ilikepython
Recognized Expert
Contributor
Join Date:
Feb 2007
Posts:
844
#2
Sep 22 '07, 11:43 PM
Originally posted by
wanstrjm
How do I get the digits out of a string?
Example: string1 = "23.45a"
I need to remove the alpha characters and keep the digits.
Like this?
[code=python]
string1 = "23.45a"
digits = [int(c) for c in string1 if c.isDigit()] # not sure about the isDigit method
#import string
#digits = [int(c) for c in string1 if c in string.digits]
[/code]
Comment
Post
Cancel
bvdet
Recognized Expert
Specialist
Join Date:
Oct 2006
Posts:
2851
#3
Sep 23 '07, 01:55 AM
This solution uses module re:[code=Python]import re
string1 = "23.45a"
string2 = "23.a"
string3 = ".45a"
patt = re.compile(r'(\ d*.\d*)')
print float(patt.sear ch(string1).gro up(1))
print float(patt.sear ch(string2).gro up(1))
print float(patt.sear ch(string3).gro up(1))[/code]
>>> 23.45
23.0
0.45
>>>
Comment
Post
Cancel
ghostdog74
Recognized Expert
Contributor
Join Date:
Apr 2006
Posts:
511
#4
Sep 23 '07, 02:14 PM
make use of existing string functions
Code:
''.join([i for i in list(s) if not i.isalpha()])
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment