# Splitting a string of numbers into smaller strings

of this:
text = ’ , 1, 8, 17, 30, 31, 34, 35 2, 5, 12, 22, 25, 27, 33, 9, 10, 11, 14, 15, 21, 29, ’
I would like these:
text1 = ‘1, 8, 17, 30, 31, 34, 35’
text2 = ‘2, 5, 12, 22, 25, 27, 33’
text3 = ‘9, 10, 11, 14, 15, 21, 29’
How can it be solved? Thanks!

Thanks! I wish a Python code would do it! Is there a solution?

Of course. Many. Depends on the details of what you want, which you’re not telling us.

My secret wish is to find a simple solution as I’m trying to slice and dice and it’s difficult. That’s it.

text1 = text[2:27]
text2 = text[29:53]
text3 = text[55:79]

Well, now you’ve already done the slicing and dicing as well.

Do you have more details to share? The “smaller strings”, do they require 7 comma separated values? It can be difficult to provide guidance without the full picture

A good place to start is having a look at `str` methods. One of the best ways to “slice and dice” a string (without slicing) is `str.split`. Note also, that a strings have concatenation method called `join`.

Python also has a regular expressions facility.

1 Like

I am happy to share the full code. But the point is that this list is constantly changing when the code is running. Therefore, there is always a difference in the slices, because it is not known where the one- and two-digit numbers are. However, this would be important for PDF saving!

`````` def szelvenyek_pdfbe(self):
"""elmenti a lottószámokat PDF -be!"""

text_szoveg = self.lotto_text.get("1.0", tk.END)
# text_szoveg = '\n9, 10, 15, 25, 27, 29, 31\n2, 8, 14, 21, 22, 30, 35\n1, 5, 6, 11, 17, 19, 33\n12, 34\n'

text_szoveg = text_szoveg.replace("\n", ", ")
# text_szoveg = ' 1, 8, 17, 30, 31, 34, 35 2, 5, 12, 22, 25, 27, 33 9, 10, 11, 14, 15, 21, 29 6, 19 '

text1 = text_szoveg[2:27]
text2 = text_szoveg[29:53]
text3 = text_szoveg[55:79]
text4 = text_szoveg[80:]

pdfbeMent.PdfMentes(text1,
text2,
text3,
text4,
self.fajlnev_entry.get())

self.szelveny_ment_label.configure(text='MENTVE!')

``````

I posted a code snippet. I formulated the point above that the text string is constantly changing. Therefore, the slicing may be inaccurate.

Strip the leading and trailing commas and spaces with `.strip` and then split it on commas with `.split`.

You now have a list of strings.

Slice the list into groups of 7 and join each group with `.join` and a comma.

1 Like

How do you know they want that? Maybe they want 3 groups. Seems more likely, even, as they want them in separate variables, and it would be bad to have a dynamic number of variables.

… Unless both things are equal because they always have exactly 21 numbers. So many details they’re keeping secret…

To solve the problem, we need to be able to understand: what is the rule that tells you, where the string should be sliced?

Thanks! No secret. There is a Hungarian lottery statistics website: Lottó 567 - Lottó statisztikák
From here I buy 4 lists of 7 numbers each. I edit these lists into a connected list. I get a list of 4x7=28 numbers. This is what I process with my program: Save to a json file, then scan and display in tkinter Text.
Extraction from Text, I mix the 28 numbers. I reduce the number repetitions to one piece, so usually 23 pieces remain. I will divide this into the numbers of the Scandinavian lottery tickets, which can be made into seven pieces, three tickets in total.
Some numbers are missing. I display these three series of numbers again in tkinter Text, then read them from here and save them to PDF.
So a problem arises when slicing.
I found the solution at night: instead of 1, 2, 3, 4, etc., 01, 02, 03, etc. I have to apply. After a little investigation, I found the solution with the help of print(help(str.zfill)) and data = (f"{1:02}").
You are a great team!