Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python, how can I execute the output in tabular form: ten code-symbol pairs in each line?

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

This is my code. the output is:

!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E

But I want to print in a console like this:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

# set chunk_size which is number of initial elements to handle per each outputted line
chunk_size = 10

def format_element(x):
    x = chr(x)
    return x if x != '\x7f' else "None"

# prepare initial list elements with output strings
ll = [f"{x} is {format_element(x)}" for x in range(32, 128)]

# split list into chunks using chunk_size
ll = [ll[i:i+chunk_size] for i in range(len(ll))[::chunk_size]]

# join inner lists into output lines strings
ll = [" ".join(x) for x in ll]

# print each line separately
for i in ll:
    print(i)

Output:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading