打印字符映射

时间:2013-03-28 21:04:34

标签: python python-3.x

我需要编写一个程序,提供下表的输出:

chr:      !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  
asc: 32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  
chr:  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  
asc: 48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  
chr:  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  
asc: 64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  
chr:  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  
asc: 80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  
chr:  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  
asc: 96  97  98  99  100 101 102 103 104 105 106 107 108 109 110 111  
chr:  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~    
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127  

任何帮助都会受到赞赏,虽然我想要求不给出完整的答案,而是提示,所以我有一些挑战。感谢。

5 个答案:

答案 0 :(得分:6)

ordchr函数可以帮助您:

ord('a')  # 97
chr(97)   # 'a'

添加到range,你就开始炖了!

答案 1 :(得分:4)

for i in range(32,128):
    print (i, chr(i))

或者更接近你想要的东西:

#!/usr/bin/python3
def f(x,y):
    for i in range(x,y):
        print ('%3d '%i,end=''),
    print()
    for i in range(x,y):
        print ('%3s '%chr(i),end='')
    print()
for x in range(32,128,16):
    f(x,x+16)

答案 2 :(得分:1)

print '''chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: @ A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127'''

答案 3 :(得分:1)

提示:在给定范围的循环中使用循环,从而将行区分为大小写。转换后的字符可以作为字符串一起添加,只有在添加完所有字符串后才打印该行。

完整的代码是:

for i in range (2,14): 
 #range(1,13 would have been correct as well, but I want to use the parity)
if i%2==0: #use even numbers for "chr..." line
  a="chr:  "
  for j in range(int((i/2-1)*16+32),int((i/2-1)*16+48)):
#range is a bit complicated due to the i-range
     b=str(chr(j))
#the following ifs are used to regulate space depending on character length
     if len(b)==1:
        s="   "
     if len(b)==2:
        s="  "
     if len(b)==3:
        s=" "
     a=a+b+s #add new characters with space to the previous ones
  print(a)
if i%2==1: #use odd numbers for asc:... line
  a="asc: "
  for j in range(int(((i-1)/2-1)*16+32),int(((i-1)/2-1)*16+48)):
     b=str(j) #in this line you need only the numbers
#the following ifs are used to regulate space depending on character length
     if len(b)==1:
        s="   "
     if len(b)==2:
        s="  "
     if len(b)==3:
        s=" "
     a=a+b+s
  print(a)

答案 4 :(得分:0)

一个好朋友给了我这个小贴士,它有用!剩下要做的就是调整占位符:

    for i in range (0, 12):
       if i%2 ==0:
         content ="chr:"
         for j in range (32, 48):
            content=content+"   "+str(chr(j+(i//2)*16))
          print (content)    
        if i%2 ==1:
          content = "asc:"
          for j in range (32, 48):
             content=content+"  "+str(j+(i//2)*16)
          print (content)