Python:如何打印字母a-z的范围?

161

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

2. a-n 中的每秒元素: a c e g i k m

3. 将 a-n 添加到 urls{hello.com /,hej.com /,...,hallo.com/} 的索引中: hello.com/a hej.com/b ... hallo.com/n


6
有趣的是,对于一个“初学者”问题,你仍然可以得到多种答案。我能够打字并不意味着我能够“用Python”,我真的很喜欢gnibbler相对于for-messy-things的答案。感谢大家的回答,并且——保持简单,特别感谢gnibbler。 - hhh
2
这不是各种各样的答案,只有两种。一种使用 rangechr(),另一种使用 string 中的现成列表,许多人可能没有想到。 - Lennart Regebro
18个回答

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

要处理URL,您可以使用以下类似代码:

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

1
我相信在Python 2.x中,string.ascii_lowercase已经可以使用,所以为了确保,请始终使用ascii_lowercase。 - johk95
1
@johk95,实际上str.lowercase是与语言环境相关的,所以一开始并不是最好的选择。我已经在我的答案中替换了它。 - John La Rooy
你好,能告诉我这只有英文版本吗?我不能获取其他语言的相同内容吗?谢谢和最好的问候。 - Alain Michael Janith Schroter

72

假设这是一份作业 ;-) - 不需要调用库等其他东西 - 它可能希望您使用range()和chr/ord,像这样:

for i in range(ord('a'), ord('n')+1):
    print chr(i),

对于其余部分,只需多使用一下range()函数即可。


28

提示:

import string
print string.ascii_lowercase
并且
for i in xrange(0, 10, 2):
    print i

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

22
for one in range(97,110):
    print chr(one)

14

获得所需数值的列表

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))
或者
import string
string.letters
string.uppercase
string.digits

这个解决方案使用了 ASCII 表ord 可以从一个字符获取 ASCII 值,而 chr 则相反。

应用列表知识

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

1
看起来在Python 3中已经移除了string.letters,只有string.ascii_letters可用,并不完全相同 - jonespm

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

并且

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

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

1
在Python 3中将其转换为元组(不可变对象):tuple(string.ascii_lowercase) - Alex Willison

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

输出

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

欢迎来到StackOverflow。请尽可能清晰地解释为什么这是对问题的完整回答。 - Jeroen Heier
谢谢。我喜欢你构建这个的方式。 - hmacias

3
list(string.ascii_lowercase)

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

3
import string

string.printable[10:36]
# abcdefghijklmnopqrstuvwxyz
    
string.printable[10:62]
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接