You have to convert the characters to numbers and back again.
for c in xrange(ord('a'), ord('z')+1):
print chr(c) # resp. print unicode(c)
For the sake of beauty and readability, you can wrap this in a generator:
def character_range(a, b, inclusive=False):
back = chr
if isinstance(a,unicode) or isinstance(b,unicode):
back = unicode
for c in xrange(ord(a), ord(b) + int(bool(inclusive)))
yield back(c)
for c in character_range('a', 'z', inclusive=True):
print(chr(c))
This generator can be called with inclusive=False (default) to imitate Python's usual bhehaviour to exclude the end element, or with inclusive=True (default) to include it. So with the default inclusive=False, 'a', 'z' would just span the range from a to y, excluding z.
If any of a, b are unicode, it returns the result in unicode, otherwise it uses chr.
import string
for char in string.ascii_lowercase:
print char
See string constants for the other possibilities, including uppercase, numbers, locale-dependent characters, all of which you can join together like string.ascii_uppercase + string.ascii_lowercase if you want all of the characters in multiple sets.
There are other good answers here (personally I'd probably use string.lowercase), but for the sake of completeness, you could use map() and chr() on the lower case ascii values:
# generating 'a to z' small_chars.
small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
# generating 'A to Z' upper chars.
upper_chars = [chr(item).upper() for item in range(ord('a'), ord('z')+1)]
Using @ned-batchelder's answer here, I'm amending it a bit for python3
def char_range(c1, c2):
"""Generates the characters from `c1` to `c2`, inclusive."""
"""Using range instead of xrange as xrange is deprecated in Python3"""
for c in range(ord(c1), ord(c2)+1):
yield chr(c)
base64chars = list(chars('AZ', 'az', '09', '++', '//'))
for a in base64chars:
print(repr(a),end='')
print('')
for a in base64chars:
print(repr(a),end=' ')
Why the list()? Without base64chars might become a generator (depending on the implementation you chose) and thus can only be used in the very first loop.
Python 2
Similar can be archived with Python 2. But it is far more complex if you want to support Unicode, too. To encourage you to stop using Python 2 in favor of Python 3 I do not bother to provide a Python 2 solution here ;)
Try to avoid Python 2 today for new projects. Also try to port old projects to Python 3 first before extending them - in the long run it will be worth the effort!
Proper handling of Unicode in Python 2 is extremely complex, and it is nearly impossible to add Unicode support to Python 2 projects if this support was not build in from the beginning.
Hints how to backport this to Python 2:
Use xrange instead of range
Create a 2nd function (unicodes?) for handling of Unicode:
Use unichr instead of chr to return unicode instead of str
Never forget to feed unicode strings as args to make ord and array subscript work properly
This works around the pesky issue of accidentally including the punctuation characters in between numbers, uppercase and lowercase letters in the ASCII table.