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

时间:2010-07-06 20:51:21

标签: python string ascii

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。附加到urls的索引{hello.com/,hej.com/,...,hallo.com /}: hello.com/a hej.com/b ... hallo.com/n < / p>

18 个答案:

答案 0 :(得分:157)

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

答案 1 :(得分:40)

假设这是一个家庭作业;-) - 不需要召唤库等 - 它可能希望你使用chr / ord的range(),如下所示:

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

对于其余部分,只需使用范围()

再玩一下

答案 2 :(得分:20)

提示:

import string
print string.ascii_lowercase

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

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

答案 3 :(得分:17)

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

答案 4 :(得分:8)

获取具有所需值的列表

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 tableord获取字符的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']

答案 5 :(得分:7)

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

答案 6 :(得分:5)

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

答案 7 :(得分:2)

#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)]

答案 8 :(得分:2)

这个问题的答案很简单,只需制作一个名为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

答案 9 :(得分:1)

尝试:

strng = ""
for i in range(97,123):
    strng = strng + chr(i)
print(strng)

答案 10 :(得分:1)

这是你的第二个问题:string.lowercase[ord('a')-97:ord('n')-97:2]因为97==ord('a') - 如果你想学习一点,你应该自己弄清楚其余部分; - )

答案 11 :(得分:1)

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

答案 12 :(得分:1)

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

答案 13 :(得分:1)

import string

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

答案 14 :(得分:0)

关于gnibbler的回答。

Zip函数,full explanation,返回a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. [...]构造称为list comprehension,非常酷的功能!

答案 15 :(得分: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

结果

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

这与其他回复有何不同

  • 迭代任意数量的基本网址
  • 循环浏览网址,直到我们用完所有字母后才停止
  • 结合使用enumerate和列表理解和str.format

答案 16 :(得分:0)

我希望这会有所帮助:

import string

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

答案 17 :(得分:0)

# Assign the range of characters
first_char_start = 'a'
last_char = 'n'

# Generate a list of assigned characters (here from 'a' to 'n')
alpha_list = [chr(i) for i in range(ord(first_char), ord(last_char) + 1)]

# Print a-n with spaces: a b c d e f g h i j k l m n
print(" ".join(alpha_list))

# Every second in a-n: a c e g i k m
print(" ".join(alpha_list[::2]))

# Append a-n to index of urls{hello.com/, hej.com/, ..., hallo.com/}
# Ex.hello.com/a hej.com/b ... hallo.com/n

#urls: list of urls
results = [i+j for i, j in zip(urls, alpha_list)]

#print new url list 'results' (concatenated two lists element-wise)
print(results)