rachelalll
rachelalll

Reputation: 122

Indexing through a string to grab all the letters after numbers appear

city = 'water1234atlantaga'
state = 'ga'

How would I be able to grab the part of the string that appears after the numbers?

Upvotes: 0

Views: 46

Answers (1)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

Try:

import re

city = 'water1234atlantaga'

res=re.findall(r"(?<=\d)[^\d]+", city)

print(res)

Outputs:

['atlantaga']

In essence:

[^\d]+ matches 1, or more non-digit character

(?<=\d) indicates, that it has to be preceded by a digit (without returning the digit itself)

The whole thing will return all the matched non-digit strings - so e.g. if you would have a1b2c it would return ['b', 'c'] if you don't care about second and following just take res[0].

Upvotes: 1

Related Questions