-1

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.

5
  • And what did you try in order to solve that? How did you try to figure things out? Commented Sep 16, 2021 at 3:06
  • "10001001010Q1002000293Q100292Q".split('Q') Commented Sep 16, 2021 at 3:08
  • FYI, calling dir() on your string, e..g s='123Q456'; dir(s) will give you a list of methods that a string supports. split and partition sound like something that divides strings. help(split) and help(partition) will give you a description of what they do. Try this out. Commented Sep 16, 2021 at 3:23
  • Welcome to Stack Overflow. I tried literally copying and pasting your question title into a search engine. I got a page full of relevant and useful results, including video tutorials. For future reference, please note that a minimum of effort like this is expected before posting. Ideally a lot more. Commented Sep 16, 2021 at 5:58
  • I searched like crazy and couldn't find anything about it. Commented Sep 16, 2021 at 14:22

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.