How can I print all possible words of a user-specified length “x” in python? I already know how to do it for a specific number of characters like all possible 3 letter words by using for loops, and don’t want a lengthy if-else ladder for each specified length. I was looking for programs that don’t use modules, rather loops.
The code I used for a definite length (3 in this case):
for i in range(ord('a'),ord('z')):
for j in range(ord('a'),ord('z')):
for k in range(ord('a'),ord('z')):
print(chr(i)+chr(j)+chr(k));
It prints all words starting from aaa to zzz, but the code starts getting very ‘bulky’ or ‘lengthy’ for more iterations.
Is there any shorter or alternative way which can print the words of a user-specified length?
Aarush Ravez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8