Python Forum
Splitting strings in python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Splitting strings in python? (/thread-15120.html)



Splitting strings in python? - NLittle17 - Jan-05-2019

I am trying to learn how to split a word into increments of 2. What I have so far is:

word = "potlucks"
i = 0
print(word[0:2])

OUTPUT:
po

** So if I want it to continue increasing by letters until the word ends what do I need to add. I believe it's something with += 2 but I don't know where exactly to add it.


RE: Splitting strings in python? - Axel_Erfurt - Jan-05-2019

word = "potlucks"
for i in range(2, len(word) + 1):
    print(word[:i])
Output:
po pot potl potlu potluc potluck potlucks



RE: Splitting strings in python? - NLittle17 - Jan-05-2019

(Jan-05-2019, 09:01 AM)Axel_Erfurt Wrote:
word = "potlucks"
for i in range(2, len(word) + 1):
    print(word[:i])
Output:
po pot potl potlu potluc potluck potlucks

I'd want it to read
po
tl
uc
ks


RE: Splitting strings in python? - Axel_Erfurt - Jan-05-2019

word = "potlucks"
for i in range(0, len(word) + 1, 2):
    print(word[i:i + 2])
Output:
po tl uc ks