I'm struggeling with a problem which probably is farely easy to solve. I just cant find a simple way to do it.

I want to be able to recognize the occurance of digits in a string. Lets say the string looks like:

file_Name1 = 'abc123de'
file_Name2 = 'qwe987rty'

Now I want to be able to pick out '123' and 987 from those strings. The numbers vary so I dont want to search for specific numbers, just find the digits..
Edit: In the end I want to be able to recognize the amount of digits (using len) but maybe there's a faster way to do this?

7 Years
Discussion Span
Last Post by Lucaci Andrew

I'd say split the string into a list and use the filter method:

file_Name1 = 'abc123de'
# store that string as a list of characters
L1 = list(file_Name1)
# call filter on it using a lambda expression
L1 = filter(lambda x: x in '1234567890', L1)
# L1 now = ['1', '2', '3']
num_digits = len(L1)

If you need to know what filter does, lookie here:

filter(function, iterable)

    Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

    Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.

    See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.

you can compare the string character by charater
and then use the function which convert the string to int. If the result is true then that perticular character is number else
it is string.
Eg. in VB
String s1="abcd145df78"
int length=len(s1)
string ss=""
for i=0 to length-1
if isnumeric(a) then
ss=ss & " " & a


Why did you post VB code? This is the Python forum. Regardless, please put it in code tags.

Edited by happygeek: fixed formatting


Shadwickmans solution using filter is a very good one!
Another way is to use regular expressions...

Without further ado, here's a simple example using regular expressions:

#example using regular expressions
import re

# here's the string we'll be searching:
myStr = "abc123def"

# search through the string using a regular expression
# this regex will return any decimal digits (0-9) it finds in the string
temp = re.search('[0-9]+', myStr)
# you could also use the following regex:
# m = re.search('\d+', myStr)
# which is another way of writing the above expression

# now lets process the results of the search...
result = temp.group()

numDigits = len(result)

print "In the string:", myStr, "- There were", numDigits, "numerical digits found, the digits were", result

The code above is a little clunky and inelegant as we're storing variables we probably don't need to be..But it is only an example!

If you only wanted to know the number of digits in the string you could shorten the above example to 3 lines of code:

# regular expressions example 2
import re


print "Number of digits =", len( (re.search('[0-9]+', myStr)).group() )

As you can see, in the above code we're munging several operations into one command at the end of the print statement.

If you want to find out more about using regular expressions check out the documentation that ships with python (hit F1 in idle!) and search for 'regular expression' you'll find out all you need to know!

Cheers for now,


Sorry to continue a thread that's been marked solved, but I thought I'd better note that the solution I posted would only match a single block of numbers.
If we changed the value of myStr to "abc123def456", the examples I previously posted would only pick up the first block of digits (123).

In case you've got more than one block of digits in the string and you want to get them all you'd need to use findall() instead of search()....(See below)

import re


result = re.findall('[0-9]+', myStr)

numDigits = 0
for i in result:

print "String:", myStr
print "Digits found:", result
print "Number of digits =", numDigits

Thought I'd better mention it! :)


Hi all,
I am facing a similar problem. I have list of strings which have datetime stamp in it. How can I find that in a string ? for example:-

string1 = "This even happened on 10/3/2011 6:03pm and same occured on 11/2/2011 5:05pm"
#output should be
date1 = 10/3/2011
time1 = 6:03pm
date2 = 11/2/2011
time2 = 5:05pm
#I want to find these date time stamps and do further processing from datetime library functions. I also want to identify if time is also present with date or not. Should I search for  "dd/dd/dddd (where d is digit) or is there any other better method ?


Edited by superbhimanshu


As a generic and quick method you could use the in keyword, to check if a string contains some characters, or substrings.

l = string1.split() #split the string on the whitespaces
for i in l: #for every string in l search for matches
    if '/' in i: print ("Date: "+i)
    elif (':' in i and ('pm' in i or 'am' in i)): print ("Time: "+i)

As a side note, if you have a question, please start your own thread asking it, rather than reviving old threads (this for example was 3 years old).

Edited by Lucaci Andrew


Also, you could use some regex expressions (I'm not that good at them), and here's an example:

import re
def getre():
    string1 = "This even happened on 10/3/2011 6:03am and same occured on 11/2/2011 5:05pm"
    time = re.findall(r"\w+?:..am|.:..pm", string1)
    date = re.findall(r"\w+../....", string1)
    for i in time: print ("Time: "+i)
    for i in date: print ("Date: "+i)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.