0

How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z'
aaaaa
aaaaA
aaaAa
aaAaa
and so on:
aaaAA
then on to b:
bbbbb
bbbbA

I can do it with numbers, something like this:

for i in range(10):
   for j in range(10):
     for k in range(10):
       print i,j,k

How does this work with letters?

4
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by kiddo39
0

Thanks for the reply. Here's part of my problem with that:

for i in range(97,122):
  for j in range(97,122):
   for k in range(97,122):
     print chr(i),chr(j),chr(k)

That type of loop will work but will take too long to run through the combos and I would also need the Caps (65,90) to run through it.

0

I can do it with numbers, something like this:

This is not true, or you did not show us that. Your code does not produce the desired output, and it is not, that it has numbers instead of characters.

I think the problem:

writing a loop with letters not numbers

is solved.

Please open a new topic with something similar:
help with simple discrete math homework.

I will gladly help.

1

May be this would help you:

def Separate (strng):
    uppers = []
    lowers = []
    for letter in strng: 
        if letter.isupper(): 
             uppers.append(letter)
        else:
             lowers.append(letter)
			
    print "Lowers:",lowers   
    print "Uppers:",uppers

Calling the function:

Separate('bbbbA')
Lowers: ['b', 'b', 'b', 'b']
Uppers: ['A']

1.Create two lists, one for uppers and another one for lowers

2.Use the boolean function "str.isupper()" to determine if a letter is in uppercase.

3. In case that you want to return the lowers and the uppers as string, you can use:

uppersstr =''.join([i for i in uppers])
lowersstr = ''.join([i for i in lowers ])
Votes + Comments
Thanks for your help
0

This is not true, or you did not show us that. Your code does not produce the desired output, and it is not, that it has numbers instead of characters.

I think the problem:

is solved.

Please open a new topic with something similar:
help with simple discrete math homework.

I will gladly help.

I put a loop up of numbers to show that I can run a loop with a given amount of chars. I can add 2 more values to it if a list of 5 is what I'm after. Running the numbers loop will produce:
0 0 0
0 0 1
0 0 2
and so on. After the suggested use of chr() I was able to write the loop to use letters instead of numbers but my original question was how to do this with uppers as well as lowers:

How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z':

At your suggestion, I will mark this as solved and start a new post.

1

Here's some clues to help you!

>>> ord('A')
65
>>> ord('a')
97
>>> ord('Z')
90
>>> ord('z')
122
>>> from string import letters
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> uppers = letters[26:]
>>> lowers = letters[:26]
>>> uppers
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> lowers
'abcdefghijklmnopqrstuvwxyz'
>>>

you cna iterate over those strings easily like this:

for lower in lowers:
    for upper in uppers:
        print lower, upper
Votes + Comments
Thanks for the help
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.