How to split a string every N character in Python

1 Answer

0 votes
string = 'python java c php javascript c++'

N = 3

lst = [string[idx:idx + N] for idx in range(0, len(string), N)]

print(lst) 



'''
run:

['pyt', 'hon', ' ja', 'va ', 'c p', 'hp ', 'jav', 'asc', 'rip', 't c', '++']

'''

 



answered Aug 7, 2022 by avibootz

Related questions

1 answer 123 views
1 answer 233 views
2 answers 126 views
1 answer 157 views
1 answer 207 views
...