Python Forum

Full Version: Splitting strings in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
word = "potlucks"
for i in range(2, len(word) + 1):
    print(word[:i])
Output:
po pot potl potlu potluc potluck potlucks
(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
word = "potlucks"
for i in range(0, len(word) + 1, 2):
    print(word[i:i + 2])
Output:
po tl uc ks