0% found this document useful (0 votes)
111 views

String - Python - How To Print Range A-Z - Stack Overflow

This document is a Stack Overflow post from 2010 asking how to print the range from a-z in Python. It received multiple responses providing solutions using the string module, chr and ord functions, and list comprehensions to generate the lowercase letters and perform additional tasks like printing every second letter or appending letters to URLs. The accepted response uses the string.ascii_lowercase attribute to generate the letters and demonstrates list slicing and zipping to complete the additional requests.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

String - Python - How To Print Range A-Z - Stack Overflow

This document is a Stack Overflow post from 2010 asking how to print the range from a-z in Python. It received multiple responses providing solutions using the string module, chr and ord functions, and list comprehensions to generate the lowercase letters and perform additional tasks like printing every second letter or appending letters to URLs. The accepted response uses the string.ascii_lowercase attribute to generate the letters and demonstrates list slicing and zipping to complete the additional requests.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

9/4/2020 string - Python: how to print range a-z?

- Stack Overflow

How are we doing? Please help us improve Stack Overflow. Take our short survey

Python: how to print range a-z?


Asked 10 years, 2 months ago Active 12 months ago Viewed 245k times

1. Print a-n: a b c d e f g h i j k l m n

111 2. Every second in a-n: a c e g i k m

3. Append a-n to index of urls{hello.com/, hej.com/, ..., hallo.com/}: hello.com/a hej.com/b ...
hallo.com/n
39
python string list ascii

edited Jun 11 '19 at 23:45 asked Jul 6 '10 at 20:51


dreftymac hhh
26.4k 23 103 163 41.3k 52 141 241

6 Odd that to a "beginner" question you can still get a variety of answers. The fact that I can type does not
mean that I can "python", I really like gnibbler's answer over for-messy-things. Thanks everyone for your
answers and -- keep things simple, special thanks to gnibbler. – hhh Dec 23 '10 at 2:06

2 It's not a wild variety of answers. It's two varieties. One use range and chr() and another the ready
made lists in string , which many people wouldn't think of. – Lennart Regebro Dec 25 '10 at 8:45

16 Answers Active Oldest Votes

>>> import string


>>> string.ascii_lowercase[:14]
191 'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'

To do the urls, you could use something like this

[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]

edited Aug 23 '17 at 22:19 answered Jul 6 '10 at 21:01


John La Rooy
247k 45 324 468

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
1 I believe string.ascii_lowercase already worked in python 2.x, so to be sure just always use
our Terms of Service.
ascii_lowercase. – johk95 Aug 23 '17 at 11:32
https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 1/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow

1 @johk95, actually str.lowercase is locale dependent so wasn't the best choice in the first place. I've

replaced it in my answer – John La Rooy Aug 23 '17 at 22:23

Hi, would be able to tell me whether is this only available in English? cant I get the same for other
languages as well? Thanks & Best Regards – Michael Schroter Apr 13 at 6:17

Assuming this is a homework ;-) - no need to summon libraries etc - it probably expect you to use
range() with chr/ord, like so:
48
for i in range(ord('a'), ord('n')+1):
print chr(i),

For the rest, just play a bit more with the range()

answered Jul 6 '10 at 23:55


Nas Banov
24.1k 6 43 65

Hints:

23 import string
print string.ascii_lowercase

and

for i in xrange(0, 10, 2):


print i

and

"hello{0}, world!".format('z')

answered Jul 6 '10 at 21:01


Wayne Werner
38.3k 20 154 238

for one in range(97,110):


print chr(one)
18
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service. answered Jul 6 '10 at 21:02
d dt itk
https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 2/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow
yedpodtrzitko
6,195 2 29 33

Get a list with the desired values

12 small_letters = map(chr, range(ord('a'), ord('z')+1))


big_letters = map(chr, range(ord('A'), ord('Z')+1))
digits = map(chr, range(ord('0'), ord('9')+1))

or

import string
string.letters
string.uppercase
string.digits

This solution uses the ASCII table. ord gets the ascii value from a character and chr vice versa.

Apply what you know about lists

>>> small_letters = map(chr, range(ord('a'), ord('z')+1))

>>> an = small_letters[0:(ord('n')-ord('a')+1)]
>>> print(" ".join(an))
a b c d e f g h i j k l m n

>>> print(" ".join(small_letters[0::2]))


a c e g i k m o q s u w y

>>> s = small_letters[0:(ord('n')-ord('a')+1):2]
>>> print(" ".join(s))
a c e g i k m

>>> urls = ["hello.com/", "hej.com/", "hallo.com/"]


>>> print([x + y for x, y in zip(urls, an)])
['hello.com/a', 'hej.com/b', 'hallo.com/c']

edited Feb 15 '15 at 20:21 answered Jun 22 '14 at 15:03


Martin Thoma
80.1k 100 450 690

It looks like string.letters was removed in Python 3 and only string.ascii_letters, not exactly the same, is
available – jonespm Feb 7 '19 at 17:50

import string
print list(string.ascii_lowercase)
8 # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
By using our site,
's', you 'u',
't', acknowledge that
'v', 'w', you'y',
'x', have 'z']
read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 3/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow

answered Feb 26 '16 at 8:24

Mikhail Makeev
140 2 5

1 To make this a tuple (which is immutable) in Python 3: tuple(string.ascii_lowercase) – Alex Willison


May 16 '17 at 13:29

import string
print list(string.ascii_lowercase)
6 # ['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']

and

for c in list(string.ascii_lowercase)[:5]:
...operation with the first 5 characters

answered Jul 21 '18 at 4:42


Miguel Silva
61 1 3

myList = [chr(chNum) for chNum in list(range(ord('a'),ord('z')+1))]


print(myList)
4
Output

['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']

edited Sep 6 '19 at 11:10 answered Sep 6 '19 at 11:03


Jeroen Heier Rakesh More
2,743 4 24 28 41 1

Welcome to StackOverflow. Try to explain more clearly why this is a complete answer to the question. –
Jeroen Heier Sep 6 '19 at 11:11

Thanks. I like it how you build this. – hmacias Apr 7 at 1:57

#1)
print " ".join(map(chr, range(ord('a'),ord('n')+1)))
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
2 Terms #2)
our of Service.
print " " join(map(chr range(ord('a') ord('n')+1 2)))
https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 4/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow
print .join(map(chr, range(ord( a ),ord( n )+1,2)))

#3)
urls = ["hello.com/", "hej.com/", "hallo.com/"]
an = map(chr, range(ord('a'),ord('n')+1))
print [ x + y for x,y in zip(urls, an)]

answered Nov 29 '13 at 16:48


carlos_lm
463 1 5 9

The answer to this question is simple, just make a list called ABC like so:

2 ABC = ['abcdefghijklmnopqrstuvwxyz']

And whenever you need to refer to it, just do:

print ABC[0:9] #prints abcdefghij


print ABC #prints abcdefghijklmnopqrstuvwxyz
for x in range(0,25):
if x % 2 == 0:
print ABC[x] #prints acegikmoqsuwy (all odd numbered letters)

Also try this to break ur device :D

##Try this and call it AlphabetSoup.py:

ABC = ['abcdefghijklmnopqrstuvwxyz']

try:
while True:
for a in ABC:
for b in ABC:
for c in ABC:
for d in ABC:
for e in ABC:
for f in ABC:
print a, b, c, d, e, f, ' ',
except KeyboardInterrupt:
pass

edited Jan 15 '17 at 23:29 answered Dec 18 '16 at 18:17


SnootierBaBoon
127 13

Try:
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.
2 strng = ""
https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 5/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow
strng
for i in range(97,123):

strng = strng + chr(i)


print(strng)

edited Feb 7 '18 at 20:25 answered Aug 13 '15 at 20:51


S. Salman Cetin Kaya Koc
481 3 18 29 2

This is your 2nd question: string.lowercase[ord('a')-97:ord('n')-97:2] because 97==ord('a') -- if


you want to learn a bit you should figure out the rest yourself ;-)
1
answered Jul 6 '10 at 21:04
Jochen Ritzel
88.9k 25 180 180

I hope this helps:

1 import string

alphas = list(string.ascii_letters[:26])
for chr in alphas:
print(chr)

answered Aug 9 '19 at 18:51


Fasih Zafar
31 3

About gnibbler's answer.

0 Zip -function, full explanation, returns a list of tuples, where the i-th tuple contains the i-th
element from each of the argument sequences or iterables. [...] construct is called list
comprehension, very cool feature!

edited Dec 25 '10 at 4:37 answered Dec 23 '10 at 2:22


hhh
41.3k 52 141 241

list(string.ascii_lowercase)

0
By using our
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
site,
's', you 'u',
't', acknowledge that
'v', 'w', you'y',
'x', have 'z']
read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 6/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow

edited Jun 22 '16 at 13:06 answered Jun 22 '16 at 5:11

DhiaTN townie
7,001 9 43 56 144 1 9

Another way to do it

0 import string
pass

aalist = list(string.ascii_lowercase)
aaurls = ['alpha.com','bravo.com','chrly.com','delta.com',]
iilen = aaurls.__len__()
pass

ans01 = "".join( (aalist[0:14]) )


ans02 = "".join( (aalist[0:14:2]) )
ans03 = "".join( "{vurl}/{vl}\n".format(vl=vjj[1],vurl=aaurls[vjj[0] % iilen]) for vjj in
enumerate(aalist[0:14]) )
pass

print(ans01)
print(ans02)
print(ans03)
pass

Result

abcdefghijklmn
acegikm
alpha.com/a
bravo.com/b
chrly.com/c
delta.com/d
alpha.com/e
bravo.com/f
chrly.com/g
delta.com/h
alpha.com/i
bravo.com/j
chrly.com/k
delta.com/l
alpha.com/m
bravo.com/n

How this differs from the other replies


iterate over an arbitrary number of base urls

cycle through the urls and do not stop until we run out of letters

use enumerate in conjunction with list comprehension and str.format

edited
By using our site, you acknowledge that you have read andJun 12 '19 at 0:14
understand answered
our Cookie Policy Jun Policy
, Privacy 12 '19 ,atand
0:08

our Terms of Service. dreftymac


26.4k 23 103 163
https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 7/8
9/4/2020 string - Python: how to print range a-z? - Stack Overflow

Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps
protect this question from spam and non-answer activity.

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/3190122/python-how-to-print-range-a-z 8/8

You might also like