1

I am wanting to loop through a string and capture 2 items each time while also incrementing through the index of the iterable. So I want to slice 2 items but increase the index by 1 every time through the loop. How can I do this?

my_string = 'teststring'

desired output = te es st ts st tr ri in ng

I have tried the following to slice the two items, but can't figure out the best way to iterate thought the index

str1 = 'teststring'
i=0
while i<10: 
    i +=1
    str2=str1[0:2]
    print(str2)
1
  • You are incrementing i just fine; you aren't using it in the slice, though. Commented Oct 10, 2022 at 13:29

5 Answers 5

2

Here is a possible solution (s is your string):

for j in range(len(s) - 1):
    print(s[j:j + 2])

Another one:

for c1, c2 in zip(s[:-1], s[1:]):
    print(c1 + c2)
Sign up to request clarification or add additional context in comments.

Comments

1

By using list comprehension you can do this in a one-liner

s = 'teststring'

r = ' '.join([s[i:i+2] for i in range(len(s)-1)])

print(r)

Comments

1
str1 = 'teststring'
result = []
for i in range(len(str1) - 1):
    result.append(str1[i:i + 2])

print(result)

output

['te', 'es', 'st', 'ts', 'st', 'tr', 'ri', 'in', 'ng']

Comments

-1

Since you are trying to move both the start and end point of the slice with each iteration, you want to use the index+length of slice for the end point.

You should iterate after the slice is done. Here is the answer with minimal changes to your code:

str1 = 'teststring'
i=0
while i<=len(str1)-2: 
    str2=str1[i:i+2]
    i += 1
    print(str2)

Comments

-1

I would use a for loop instead of a while loop, like this:

def strangeSlice(string):
    out = ""
    for i in range(len(string)-1):
        out += string[i:i+2]
        if i != len(string)-2:
            out += " "
    print(out)
    return out
def main():
    strangeSlice("teststring")
main() #you need to call the main function to run

1 Comment

I couldn't get this to work, but thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.