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)

Recommended Answers

All 4 Replies

Member Avatar for masterofpuppets

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 :)

Member Avatar for masterofpuppets

I wrote a code snippet for this last year :)

yeah nice....that's a shorter way of doing it :)

you can use list comprehensions

g = "ThisIsATestt"
print [g[i:i+2] for i in range(0,len(g),2)]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.