# Splitting a string of numbers into smaller strings

**URL:** <https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161>\
**Category:** Python Help\
**Created:** [July 14, 2024, 2:40pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161 "2024-07-14T14:40:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 2:40pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/1 "2024-07-14T14:40:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [July 14, 2024, 2:58pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/2 "2024-07-14T14:58:50Z")

</div>

You have already done it…

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 3:03pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/3 "2024-07-14T15:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [July 14, 2024, 3:12pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/4 "2024-07-14T15:12:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 3:22pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/5 "2024-07-14T15:22:07Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [July 14, 2024, 4:10pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/6 "2024-07-14T16:10:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![will\_f](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/will_f/32/19106_2.png) [@will\_f](https://discuss.python.org/u/will_f)\
**Post date:** [July 14, 2024, 5:16pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/7 "2024-07-14T17:16:33Z")

</div>

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](https://docs.python.org/3/library/stdtypes.html#string-methods). One of the best ways to “slice and dice” a string (without [slicing](https://docs.python.org/3/library/functions.html#slice)) is [`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split). Note also, that a strings have concatenation method called `join`.

Python also has a [regular expressions facility](https://docs.python.org/3/library/re.html#module-re).

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 5:31pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/8 "2024-07-14T17:31:00Z")

</div>

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!

 ![példa1](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/e/9/e97504cc468f6e4a208ecce4d4cbe0f57616cd4d.png)

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 5:35pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/9 "2024-07-14T17:35:55Z")

</div>

```python
 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!')

```

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 14, 2024, 5:51pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/10 "2024-07-14T17:51:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 14, 2024, 5:59pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/11 "2024-07-14T17:59:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [July 14, 2024, 6:45pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/12 "2024-07-14T18:45:27Z")

</div>

> [@MRAB](#):
>
> Slice the list into groups of 7

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.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [July 14, 2024, 6:48pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/13 "2024-07-14T18:48:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 14, 2024, 7:39pm UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/14 "2024-07-14T19:39:20Z")

</div>

> [@Layos](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![Layos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/layos/32/23330_2.png) [@Layos](https://discuss.python.org/u/Layos)\
**Post date:** [July 15, 2024, 10:37am UTC](https://discuss.python.org/t/splitting-a-string-of-numbers-into-smaller-strings/58161/15 "2024-07-15T10:37:46Z")

</div>

Thanks! No secret. There is a Hungarian lottery statistics website: [Lottó 567 - Lottó statisztikák](https://www.lotto567.hu/skadinvan_lotto/gyakorisag)  
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!

 ![Képernyőkép 2024-07-15 123706](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/9/c/9cc353e85b142c149237dcc25ba9c884daa9bafd.png)
