How to do a range with letters of the alphabet in python?

Asked

Viewed 13,058 times

7

In PHP, when we want to make a array alphabet letters, we can use the function range.

Example in PHP:

$letras = range('a', 'z');

Upshot:

Array (
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z )

And it’s also possible to do this through for

for ($a = 'a'; $a != 'aa'; $a++) echo $a;

Upshot:

abcdefghijklmnopqrstuvwxyz

But in Python, when I try to do this through the function range, an error is returned.

Python example:

 range('a', 'z');

Traceback (Most recent call last): File "", line 1, in Typeerror: range() integer end argument expected, got str.

So the question is: As simple as possible, how could you generate a list of letters of the alphabet in python?

3 answers

14


import string
a = list(string.ascii_lowercase)

print a

Upshot:

['a', 'b', 'c', ’d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', ’m', 'n', 'o', 'p', 'q', 'r', ’s', t', 'u', 'v', 'w', 'x', 'y', 'z']

Source: Stackoverflow - Alphabet range python

If you don’t want the whole alphabet you can do so:

import string
a = list(string.ascii_lowercase[:14])

print a

Or still using range() and ord():

a =[]

# list de a-n
for i in range(ord('a'), ord('n')+1):
    a.append(chr(i))

print a

Result for both cases:

['a', 'b', 'c', ’d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', ’m', 'n']

Source: Stackoverflow - Python: how to print range a-z?

  • 3

    Stop it! Damn, that surprised me! I’m going to migrate for good python

  • 1

    Hahaha.. This also surprised me :D

  • You need to know the web frameworks, I suggest starting with Flask. PHP never again :P

  • Note that in general, you don’t even need to create this list- you can use the.ascii_lowercase string directly - since strings work as strings.

  • Hahaha also surprised me :D

1

Create list from 'A' to 'ZZ' (702 widgets)

Create an empty list to use the append and Insert function, also create a counter variable to mark the position of the first characters.

alfabeto = []
c = 0
for i in range(ord('A'), ord('Z')+1):
    alfabeto.insert(c, chr(i))
    c += 1
    for j in range(ord('A'), ord('Z')+1):
        alfabeto.append(chr(i)+chr(j))

The loop consists of inserting the characters from A to Z in the first positions of the list and adding the other characters at the end following the natural order.

Create list from 'A' to 'ZZZ' (18278 widgets)

Reciprocal example of the above function, one more counter variable needed to insert the elements in the correct position from AA to ZZ.

alfabeto = []
c = 0
d = 0
for i in range(ord('A'), ord('Z')+1):
    alfabeto.insert(c, chr(i))
    for j in range(ord('A'), ord('Z')+1):
        d += 1
        alfabeto.insert(c+d, chr(i)+chr(j))
        for k in range(ord('A'), ord('Z')+1):
            alfabeto.append(chr(i)+chr(j)+chr(k))
    c += 1

In this case we can observe the need for the A~Z counter to be positioned at the end of the 'j and k' loops'.

Obviously, used idea of the solution of Math , who answered above. :)

  • This answer is correct but it is important a little text describing what is happening.

  • Explain what’s going on.

0

I like to use the range with the ord, as already quoted

#python3.*
abc=[]
for k in range(ord('a'), ord('z')+1):
  abc.append(chr(k))
print(abc)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.