I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
CodePudding user response:
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
CodePudding user response:
The syntax is str.split("separator")
.
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', '']
.
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]