7
How can I separate a string by letters and numbers at the same time?
For example if I have strings:
composto1 = 'H2SO4'
composto2 = 'CaCO2'
composto3 = 'C20H17'
I’ve tried to do:
import re
list_composto1 = re.findall('[A-Z][^A-Z]*', composto1)
list_composto2 = re.findall('[A-Z][^A-Z]*', composto2)
list_composto3 = re.findall('[A-Z][^A-Z]*', composto3)
print(list_composto1 , list_composto2 , list_composto3)
output
['H2', 'S', 'O4'] ['Ca', 'C', 'O2'] ['C20', 'H17']
However, I was only able to separate by the capital letters. Would have some method to separate the numbers together and get the result below:
list_composto1 = ['H', '2', 'S', 'O', '4']
list_composto2 = ['Ca', 'C', 'O' , '2']
list_composto3 = ['C', '20', 'H', '17' ]