Python Forum
Splitting strings in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting strings in python?
#1
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.
Reply
#2
word = "potlucks"
for i in range(2, len(word) + 1):
    print(word[:i])
Output:
po pot potl potlu potluc potluck potlucks
Reply
#3
(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
Reply
#4
word = "potlucks"
for i in range(0, len(word) + 1, 2):
    print(word[i:i + 2])
Output:
po tl uc ks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 822 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,827 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,556 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Python: if 'X' in 'Y' but with two similar strings as 'X' DreamingInsanity 6 3,906 Feb-01-2019, 01:28 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 4,270 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020