For example, I have a variable with a string
a = "Hello World" and I need what would output
he ll o wo rl d For example, I have a variable with a string
a = "Hello World" and I need what would output
he ll o wo rl d for i in range(0, len(a), 2): print(a[i:i+2]) or so
from itertools import zip_longest print('\n'.join(''.join(i) for i in zip_longest(*([iter(a)] * 2), fillvalue=''))) As a perverted alternative:
from io import StringIO a = StringIO("Hello World") while True: s = a.read(2) if not s: break print(s) string = 'hello world' for num in range(0, len(string), 2): print(string[num : num + 2]) Source: https://ru.stackoverflow.com/questions/942553/
All Articles