0

with the function:
list(string)
you put every letter from the string in a list,
I want to put each 2 letters in a list
so If I had the string: ThisIsATestt

I want to get a list:

(There will never be a letter alone, so you don't have to think about that)

4
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by jice
0

try this:

g = "ThisIsATestt"
count = 0
tmp_s = ""
l = []

for i in range( 0, len( g ) + 1, 2 ):
    if i != 0:
        while count < i:
            tmp_s += g[ count ]
            count += 1
        l += [ tmp_s ]
        tmp_s = ""

print l

note that you can specify the length of each segment by simply changing the increment value, i.e '2' in the range function
hope that helps. if not i'll post details on my solution :)

Edited by masterofpuppets: n/a

1

you can use list comprehensions

g = "ThisIsATestt"
print [g[i:i+2] for i in range(0,len(g),2)]

Edited by jice: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.