D
D
Dmitry2018-04-06 10:36:20
Python
Dmitry, 2018-04-06 10:36:20

How to divide letters in words into pairs?

I am writing a program for encrypting text in Python, the algorithm requires initially to divide the letters in the words of the text into pairs (Example: "Hello world" -> 'Pr', ''iv, 'et', 'm', 'ir'). Then we turn to the first element from the first pair, then to the second and so on, moving on to the second pair.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pcdesign, 2018-04-06
@DmitryKyd

>>> import re
>>> re.findall('..?','Hello world')
['R', 'eve', 'em', 'm', 'ir']

A
Anton Fedoryan, 2018-04-06
@AnnTHony

s = "Привет мир"
[s[i:i+2] for i in range(0, len(s), 2)]
# ['Пр', 'ив', 'ет', ' м', 'ир']

S
spikejke, 2018-04-06
@spikejke

textwrap.wrap('Hello world' , 2)
['R', 'e', ​​'et', 'mi', 'r']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question