string - Python:如何打印范围a-z?
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 {hello.com/,hej.com/,...,hallo.com/}:hello.com/a hej.com/b ... hallo.com/n
hhh asked 2019-09-17T22:48:16Z
13个解决方案
156 votes
>>> import string
>>> string.ascii_lowercase[:14]
'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'
要做网址,你可以使用这样的东西
[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]
John La Rooy answered 2019-09-17T22:48:29Z
39 votes
假设这是一个家庭作业;-) - 不需要召唤库等 - 它可能希望你使用chr / ord的range(),如下所示:
for i in range(ord('a'), ord('n')+1):
print chr(i),
对于其余的,只需使用范围()玩一点
Nas Banov answered 2019-09-17T22:48:59Z
20 votes
提示:
import string
print string.ascii_lowercase
和
for i in xrange(0, 10, 2):
print i
和
"hello{0}, world!".format('z')
Wayne Werner answered 2019-09-17T22:49:25Z
17 votes
for one in range(97,110):
print chr(one)
yedpodtrzitko answered 2019-09-17T22:49:42Z
8 votes
获取包含所需值的列表
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表。 chr获取字符的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']
Martin Thoma answered 2019-09-17T22:50:20Z
7 votes
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']
Mikhail Makeev answered 2019-09-17T22:50:37Z
5 votes
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
Welligton Miguel answered 2019-09-17T22:50:55Z
2 votes
#1)
print " ".join(map(chr, range(ord('a'),ord('n')+1)))
#2)
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)]
carlos_lm answered 2019-09-17T22:51:12Z
2 votes
这个问题的答案很简单,只需制作一个名为ABC的列表:
ABC = ['abcdefghijklmnopqrstuvwxyz']
无论何时你需要引用它,只需:
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)
也试试这打破你的设备: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
SnootierBaBoon answered 2019-09-17T22:51:48Z
1 votes
这是你的第二个问题:string.lowercase[ord('a')-97:ord('n')-97:2]因为97==ord('a') - 如果你想学习一点,你应该自己弄清楚其余的;-)
Jochen Ritzel answered 2019-09-17T22:52:13Z
1 votes
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']
townie answered 2019-09-17T22:52:30Z
1 votes
尝试:
strng = ""
for i in range(97,123):
strng = strng + chr(i)
print(strng)
Cetin Kaya Koc answered 2019-09-17T22:52:54Z
0 votes
关于gnibbler的答案。
Zip -function,完整解释,返回a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. [...]构造称为列表理解,非常酷的功能!
hhh answered 2019-09-17T22:53:24Z