Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a letter range in python?

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

for x in range(a,h,)
like image 688
derpyherp Avatar asked Feb 17 '13 22:02

derpyherp


People also ask

Can we print range in Python?

But Python does have a built-in reversed function. If you wrap range() inside reversed() , then you can print the integers in reverse order. range() makes it possible to iterate over a decrementing sequence of numbers, whereas reversed() is generally used to loop over a sequence in reverse order.

Is there an alphabet function in Python?

The isalpha() function is a built-in function used for string handling in python, which checks if the single input character is an alphabet or if all the characters in the input string are alphabets.

How do you find the range of A to Z in Python?

Use string. ascii_lowercase to print "a" to "z" ascii_lowercase to get a string of the alphabet in lowercase. To print a range within "a" to "z" , use string slicing. Further Reading: String slicing is a concise way to return fragments of strings.

How do you find the range of a character in python?

You can get a range of characters(substring) by using the slice function. Python slice() function returns a slice object that can use used to slice strings, lists, tuples. You have to Specify the parameters- start index and the end index, separated by a colon, to return a part of the string.


1 Answers

Something like:

[chr(i) for i in range(ord('a'),ord('h'))]

Will give a list of alphabetical characters to iterate through, which you can then use in a loop

for x in [chr(i) for i in range(ord('a'),ord('h'))]:
    print(x)

or this will do the same:

for x in map(chr, range(*map(ord,['a', 'h']))):
    print(x)
like image 78
Emanuele Paolini Avatar answered Sep 22 '22 20:09

Emanuele Paolini