-2

I'm trying to make a basic encryption (yes i know it's unsafe etc). I need to split up a random integer into pairs. I'd like to have each pair assigned to a variable. For example:

digits = 12345678

Should be split up into

pair1 = 12
pair2 = 34
pair3 = 56
pair4 = 78

How do I do that? (I'm new to coding so please ELI5)

  • 3
    You could convert to string and use indexing. Or use division/mod: `12345678 % 100 = 78`.... – 001 May 23 '19 at 14:07
  • 1
    Possible duplicate of [Splitting a string into 2-letter segments](https://stackoverflow.com/questions/12491537/splitting-a-string-into-2-letter-segments) – Amira Bedhiafi May 23 '19 at 14:10
  • Possible duplicate of [Split string every nth character?](https://stackoverflow.com/questions/9475241/split-string-every-nth-character) – wwii May 23 '19 at 14:15

2 Answers2

0

Stringy solution:

s = str(1234567890)

def processing(s):
  i=0
  while i < len(s):
    yield s[i:i+2]
    i = i+2

[x for x in processing(s)]

Output:

['12', '34', '56', '78', '90']

If you want it as a string with spaces separating the integer pairs:

" ".join([x for x in processing(s)])
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
0
def splitInt(integer, interval=2):
    integer = str(integer)
    newList = []
    for ditget in range(0, len(integer), interval):
        newList.append(int(integer[ditget:ditget + interval]))
    return newList