Reputation: 3
I am trying to make this ASCII list with rows of 10 and 1 space apart in each row. I cannot make them separate without messing up the table. I want it to not spit out a long row of characters.
The table is supposed to look like this.
! " # $ % & ' ( ) *
+ , - . / 0 1 2 3 4
5 6 7 8 9 : ; < = >
? @ 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 [ \
] ^ _ ` 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
{ | } ~
Here is my code:
for v in range(33,127):
if v <= 42:
print(chr(v), end = ' ')
elif v >= 43 and v <= 52:
print(chr(v), end = ' ')
elif v >= 53 and v <= 62:
print(chr(v), end = ' ')
elif v >= 63 and v <= 72:
print(chr(v), end = ' ')
elif v >= 73 and v <= 82:
print(chr(v), end = ' ')
elif v >= 83 and v <= 92:
print(chr(v), end = ' ')
elif v >= 93 and v <= 102:
print(chr(v), end = ' ')
elif v >= 103 and v <= 112:
print(chr(v), end = ' ')
elif v >= 113 and v <= 122:
print(chr(v), end = ' ')
elif v >= 123 and v <= 127:
print(chr(v), end = ' ')
else:
break
Upvotes: 0
Views: 2409
Reputation: 2436
I wanted to learn about the .format
string formatting in python3 , so I came up with a one-liner to do the trick.
print(5*' '+(16*'{:#4x}').format(*range(16)),
*['{:#4x}|'.format(i*16) + (16*'{:4c}').format(*range(16*i,16*(i+1)))
for i in range(2,8)], sep="\n")
Output:
0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf
0x20| ! " # $ % & ' ( ) * + , - . /
0x30| 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
0x40| @ A B C D E F G H I J K L M N O
0x50| P Q R S T U V W X Y Z [ \ ] ^ _
0x60| ` a b c d e f g h i j k l m n o
0x70| p q r s t u v w x y z { | } ~
Not exactly what was ordered, but a little more aesthetic in my opinion.
Explanation: '{:#4x}'.format(val)
outputs val
(a supposed int
) formatted as hexidecimal (x
) with the 0x
before it (#
) in a field 4 characters wide (4
). 16*'{:4c}'
creates 16 fields, 4 characters wide, which format whatever int
they get as a char
, which are provided by the iterable unpacking expression *range(...)
.
Upvotes: 0
Reputation: 165
Here is the code :
start = 33
end = 127
for v in range(start, end):
if (start - v) % 10 == 0: # check if (start - v) is a multiple of 10
print("")
print(chr(v), end=' ')
Upvotes: 2
Reputation: 992787
Python is a pretty flexible language. This will get you most of what you want:
print "\n".join(" ".join(map(chr, range(x, x+10))) for x in range(33, 128, 10))
This extends past your desired maximum value of 127 on the last row. Fixing that is left as an exercise for you.
Upvotes: 1