Is it possible to make a letter range in python?

前端 未结 8 1628
误落风尘
误落风尘 2021-01-02 17:23

Is there a way to do a letter range in python like this:

for x in range(a,h,)
8条回答
  •  暖寄归人
    2021-01-02 17:45

    You can use ord() to convert the letters into character ordinals and back:

    def char_range(start, end, step=1):
        for char in range(ord(start), ord(end), step):
            yield chr(char)
    

    It seems to work just fine:

    >>> ''.join(char_range('a', 'z'))
        'abcdefghijklmnopqrstuvwxy'
    

提交回复
热议问题