| | |
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: 128
Reputation:
Solved Threads: 31
•
•
•
•
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...
Views: 592 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython







