| | |
writing a loop with letters not numbers
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 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:
How does this work with letters?
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:
Python Syntax (Toggle Plain Text)
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?
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
Thanks for the reply. Here's part of my problem with that:
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.
Python Syntax (Toggle Plain Text)
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)
•
•
Join Date: Jun 2008
Posts: 125
Reputation:
Solved Threads: 30
•
•
•
•
I can do it with numbers, something like this:
I think the problem:
•
•
•
•
writing a loop with letters not numbers
Please open a new topic with something similar:
help with simple discrete math homework.
I will gladly help.
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Solved Threads: 1
May be this would help you:
Calling the function:
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:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
uppersstr =''.join([i for i in uppers]) lowersstr = ''.join([i for i in lowers ])
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 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.
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':
Here's some clues to help you!
you cna iterate over those strings easily like this:
python Syntax (Toggle Plain Text)
>>> 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:
python Syntax (Toggle Plain Text)
for lower in lowers: for upper in uppers: print lower, upper
Last edited by jlm699; May 22nd, 2009 at 10:38 am.
![]() |
Similar Threads
- help writing pseudocode, PLEASE!!!! (C++)
- ->ToLower() (Mixing letters and numbers) (C++)
- Letters, numbers and print numbers on the screen (C++)
- Textbox allows letters and numbers only (Visual Basic 4 / 5 / 6)
- random letters/numbers (Visual Basic 4 / 5 / 6)
- 3 letters or numbers com/net/org/info/biz/us -or- YPN invite [wanted] (Websites for Sale)
Other Threads in the Python Forum
- Previous Thread: How to include loops in independent python programs
- Next Thread: Fun Questions...
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt







