1

Suppose -

string = "abcdefgh"

If I do -

for i in string:
    print (i)

I get -

a
b
c
d
e
f
g

What I want is something like -

ab
bc
cd
de
ef
fg

Or in any other grouping we specify. Is it possible to make a function for this keeping in mind the grouping we require? Thanks

Menon A.
  • 580
  • 1
  • 4
  • 12
  • Related: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – TerryA May 13 '17 at 04:06

4 Answers4

5

You can use zip():

>>> for i, j in zip(string, string[1:]):
...     print(i+j)
... 
ab
bc
cd
de
ef
fg
gh

As a function:

def func(seq, n):
    return [''.join(item) for item in zip(*[seq[n:] for n in range(n)])]

Example:

>>> for item in func("abcdefgh", 3):
...     print(item)
... 
abc
bcd
cde
def
efg
fgh
TerryA
  • 58,805
  • 11
  • 114
  • 143
1

if s is the name of your string, this comprehension will do what you want:

[s[i:i+2] for i in range(0, len(s) - 1)]

Using this, you can easily print the strings on separate lines:

for substr in [s[i:i+2] for i in range(0, len(s) -1)]:
  print substr

It can be generalised fairly easily:

def subgroups(s, n):
    return [s[i:i+n] for i in range(0, len(s) - 1)]

(and this function can similarly be used to print the resulting substrings in any fashion you like)

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
0

This works:

string = "abcdefgh"

i = 0
while i < len(string) - 1:
    print(string[i]+string[i+1])
    i += 1

Result:

ab
bc
cd
de
ef
fg
gh

If you don't want gh (it's missing in your example), change the while loop to: while i < len(string) - 2:.

Also another way to do (which hasn't been posted), is via regex:

import re

print("\n".join(re.findall(r'(?=(\w\w))', 'abcdefgh')))

The (?=) (lookahead assertion), allows regex patterns to overlap.

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
-1
import re
def splittext(text, split_by):
    '''the regex will take a string and create groupings of n-characters plus a final grouping of any remainder. if no remainder is desired, the |.+ can be removed''' 
    return re.findall(r".{%d}|.+" % split_by, text)

ret = splittext("HELLOWORLD!", 2)
print "\n".join(ret)

some sample output

>>> re.findall(r".{2}",a)
['HE', 'LL', 'OW', 'OR', 'LD']
>>> re.findall(r".{2}|.{1}",a)
['HE', 'LL', 'OW', 'OR', 'LD', '!']
>>> re.findall(r".{2}|.*",a)
['HE', 'LL', 'OW', 'OR', 'LD', '!', '']
>>> re.findall(r".{2}|.+",a)
['HE', 'LL', 'OW', 'OR', 'LD', '!']
>>> print "\n".join(_)
HE
LL
OW
OR
LD
!
>>>    
pyInTheSky
  • 1,459
  • 1
  • 9
  • 24
  • Clever. Unfortunately, clever is usually a sign of a bad idea. – Jon Kiparsky May 13 '17 at 04:11
  • Your initial example was almost correct, you just needed to add `overlapped=True`, see [here](http://stackoverflow.com/a/18966698/1695437). Alternatively, you could use a lookahead assertion (`(?=)`). – Darkstarone May 13 '17 at 04:15
  • 1
    @Darkstarone the .+ will pull any remainder ... wasn't sure if the OP wanted only full pairs or pairs + remainder – pyInTheSky May 13 '17 at 04:16