8
test1 = 'name1'
test2 = 'name2'
..
test3 = 'name45'
test4 = 'name1231231'

Let's say I have bunch of strings which start with 'name' and are followed by a number of any length.

How can I parse out the number in the string?

Is regex the only way or is there a built-in module that can accomplish this task?

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
ealeon
  • 12,074
  • 24
  • 92
  • 173
  • 6
    I did not downvote but perhaps your post was downvoted because of one of these [reasons](http://i.stack.imgur.com/Umcsc.png) – Bhargav Rao Mar 14 '16 at 16:57
  • Loop over all of them, replace `name` and cast `int()` or `float()` on them. – jhoepken Mar 14 '16 at 16:57
  • int_testi=int(testi[4:len(testi)]) would do. – IronMan007 Mar 14 '16 at 16:59
  • 1
    its pretty clear, and there is research via regex, how is it not useful? this can be helpful to others – ealeon Mar 14 '16 at 17:00
  • 2
    If you have the strings like this `test=['name1','name2','name123125']` you can get just the integers like this: `ints = [int(n[4:]) for n in test]` – avip Mar 14 '16 at 17:02
  • this question is different from someone marking as duplicate. this has a specific format of numbers being the last and finding ways to do it and the answered regex wont work in this format – ealeon Mar 14 '16 at 17:02
  • @karthikr : it is not the same. – IronMan007 Mar 14 '16 at 17:02
  • @karthikr remove duplicate if you agree – ealeon Mar 14 '16 at 17:03
  • The idea is the same you just need a boundary for end of pattern.. But yes, the answer provided is better - so removed the dup mark – karthikr Mar 14 '16 at 17:04
  • How about [Extract Number from String - Python](http://stackoverflow.com/q/26825729) or [How to get integer values from a string in Python?](http://stackoverflow.com/q/11339210) – Bhargav Rao Mar 14 '16 at 17:06

2 Answers2

34

In Python 3, you could do the following:

import string

for test in ['name1', 'name2', 'name45', 'name1231231', '123test']:
    print(int(test.strip(string.ascii_letters)))

Giving you:

1
2
45
1231231
123

string.ascii_letters gives you a string containing all upper and lowercase letters. Python's strip() function takes a string specifying the set of characters to be removed, which in this case is all alpha characters, thus leaving just the numbers behind.

Note: This would not be suitable for a string such as 123name456.


If there are just known common prefixes/suffixes to all strings, the following approach could also be used in Python 3.9:

for test in ['name1', 'name2', 'name45', 'name1231231', '123test']:
    print(test.removeprefix('name').removesuffix('test'))
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
5

If you know that the prefix is name, then you can either remove just that string, or you can skip the first four letters, like so:

s = 'name123'
print int(s.replace('name',''))

s = 'name123'
print int(s[4:])
Robᵩ
  • 163,533
  • 20
  • 239
  • 308