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?

Recommended Answers

All 7 Replies

You can use the magic of ascii:)

ord("a")==97
chr(97)=="a"

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.

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.

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 ])
commented: Thanks for your help +1

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.

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
commented: Thanks for the help +1

jlm699, thank you! That helped alot!

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.