0
  • I am trying to print string and its corresponding decimal ascii value directly below it. This seems like a simple task but after copious research I have not yet found a solution to my issues. Using the end=" "argument to the print function did not work, and neither did anything else I found on stackoverflow. In its current state it prints vertically, down the command prompt, which is not what I want. I need it to print horizontally.
string = "hello world"
for ch in string:
  print(f'''
        {ch}
        {ord(ch)}
        ''')
  • Desired Output:
h   e   l   l   o        w   o   r   l   d
104 101 108 108 111  32  119 111 114 108 100 

Actual Output:

h
104

e
101

... ... trimmed for brevity ... ...

l
108

d
100
  • I know that somewhere along the way I would have to worry about the width of the top line being smaller than the width of the bottom line so that characters match up nicely, but assume I am not worrying about this for now.
BitWrecker
  • 174
  • 1
  • 10
  • 2
    You could make two temporary strings, which you concatenate to inside the loop, and afterwards print the two strings after each other. – dnorhoj Apr 19 '22 at 19:07

1 Answers1

2

One approach could be to build out the ord values using a list comprehension, and then iterate over each char and ord pair in the string, and then print out each char padded to the string length of each ord - i.e. a numeric value of 102 would mean a pad width of 3.

string = "hello world"
ords = [ord(ch) for ch in string]

for ch, ord_ in zip(string, ords):
    len_ord = len(str(ord_))
    print(ch.ljust(len_ord), end=' ')

print()
print(*ords)

Result:

h   e   l   l   o      w   o   r   l   d   
104 101 108 108 111 32 119 111 114 108 100

If you need to split this off into chunks, for example if your string is too long and you need it to fit your terminal or window, you can use an approach as mentioned here in order to split the string into chunks, and still keep the same logic above.

Here's an example of how that could work. Note that you'll need to adjust chunk_size based on width of your terminal window.

string = "hello world, how is it going there?"
ords = [ord(ch) for ch in string]

# TODO: can set this based on your terminal width
chunk_size = 10

for i in range(0, len(string), chunk_size):
    ords_chunk = ords[i:i + chunk_size]
    for ch, ord_ in zip(string[i:i + chunk_size], ords_chunk):
        len_ord = len(str(ord_))
        print(ch.ljust(len_ord), end=' ')
    print()
    print(*ords_chunk)

Output:

h   e   l   l   o      w   o   r   l   
104 101 108 108 111 32 119 111 114 108
d   ,     h   o   w      i   s      
100 44 32 104 111 119 32 105 115 32
i   t      g   o   i   n   g      t   
105 116 32 103 111 105 110 103 32 116
h   e   r   e   ?  
104 101 114 101 63
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
  • This just prints out the letters and the the strings... If the input were multiple lines long this would not work because I am looking to it to be `print(line_of_chars + "\n" + line_of_ords)` for every line in the input text. – BitWrecker Apr 19 '22 at 21:03
  • So in other words, the lines should alternate between printing the characters, then the corresponding ords for the characters that were just printed, and then if there's more characters left print the next set of characters, then the next set of ords, and so on.. – BitWrecker Apr 19 '22 at 21:05
  • nevermind, your method does indeed work... its just when the text input is too big it doesnt break across new lines, and my terminal window is not big enough even when made to be the entire size of my screen. Is there a way to partition this off into chucks easily? – BitWrecker Apr 19 '22 at 21:14
  • @BitWrecker I see what you mean. I guess i didn't see the overflow issue because I was only testing with small strings like "hello world" as in the example above. – rv.kvetch Apr 19 '22 at 22:08
  • 1
    @BitWrecker note that I've updated my answer above. I hope that should now work how you were wanting it to - let me know if so. – rv.kvetch Apr 19 '22 at 22:31
  • 1
    That worked beautifully, thank you so much! Exactly what I was looking for! – BitWrecker Apr 20 '22 at 20:11