Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to slice from abcd to abc bcd in Python

There’s an string like this:

abcdefghijk

I want to slice it like this:

abc
bcd
cde
def
efg
fgh
ghi
hij
ijk

I googled but could not find any site or answers to reach this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update 1

My question is not similar to Split string every nth character?.

>Solution :

s = "abcdefghijk"
str_list = []
for i in range(len(s)-2):
    new_string = s[i] + s[i+1] + s[i+2]
    str_list.append(new_string)

for x in str_list:
    print(x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading