Reputation: 580
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
Upvotes: 1
Views: 1219
Reputation: 60014
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
Upvotes: 5
Reputation: 1479
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
!
>>>
Upvotes: -1
Reputation: 4730
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.
Upvotes: 0
Reputation: 7753
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)
Upvotes: 1