Given a string containing a sequence of letters and numbers (such as):
"GHG-H89KKK90KKP"
Do we have any way to split this string into two lists, one containing the letters and one containing the numbers?
["GHG-H", "KKK", "KKP"]
[89,90]
>Solution :
Use re.findall from the re (regular expressions) library to find patterns in the string. The first expression (?i)[a-z|\-]+ finds all sequences of letters (ignoring case), it also includes hyphens (-). The second expression [0-9]+ finds all sequences of numbers in your string.
import re
string = "GHG-H89KKK90KKP"
print(re.findall("(?i)[a-z|-]+", string))
print(re.findall("[0-9]+", string))
Output:
['GHG-H', 'KKK', 'KKP']
['89', '90']