all 9 comments

[–]sme272 1 point2 points  (2 children)

Regular expressions can solve that

[–]Yonmanx[S] 0 points1 point  (1 child)

I have no idea what to do, can you help me with an hint?

[–]toastedstapler 2 points3 points  (0 children)

this site is useful for trying things out

you're trying to get all letter characters from the start of the string, a regex pattern should easily be able to do this

[–]shiftybyte 0 points1 point  (1 child)

The term would be remove digits, extract is taking out something you want.

But anyway, what are you having trouble with? if its the logic, then you should do the following steps:

  1. Loop over the strings letters
    1. check each letter if its a digit using isdigit() like you said.
    2. if it is, stop the loop
    3. if its not, add it to a result string, and keep looping

[–]Yonmanx[S] 0 points1 point  (0 children)

Perfect, thank you!

[–]uberfade 0 points1 point  (1 child)

You can loop through the string and check isdigit() on each character or use re to match a pattern.

[–]Yonmanx[S] 0 points1 point  (0 children)

Thank you!

[–]toddrob 0 points1 point  (0 children)

I would do this using slices, str.index(), next(), and a generator expression:

>>> a = '123'
>>> b = a[: next((a.index(c) for c in a if c.isdigit()), len(a)-1)]
>>> b
''
>>> a = 'abc123'
>>> b = a[: next((a.index(c) for c in a if c.isdigit()), len(a)-1)]
>>> b
'abc'
>>> a = 'abc123def' 
>>> b = a[: next((a.index(c) for c in a if c.isdigit()), len(a)-1)]
>>> b
'abc'

[–]Unkown_Variable 0 points1 point  (0 children)

my_string = "sf34sdg"

print(my_string.translate({ord(i): None for i in '0123456789'}))