Home > Software engineering >  How to split text into columns?
How to split text into columns?

Time:11-18

enter image description here

I want to split these ascii characters into 4 columns so it will look more convenient.. Uploaded a picture as an example..

for i in range(1,121):
    a = chr(i)
    print(str(i) ". " str(a))


I have tried the .format or split(), but they don't seem to work as intended

CodePudding user response:

Because print prints line-by-line, we're going to have to figure out which characters go on the same line, and then format those into a string. Since some characters are not printable, we'll have to replace them, especially characters like "\n" and "\t", which would break our formatting. Luckily, python provides a str.isprintable method that tells us exactly this. If a character is not printable, I've replaced it with an uppercase X (but you can choose any other character you want).

Next, we need to play with string formatting. Here's a cheatsheet that describes the f-string syntax I've used. E.g., f"{num:>3}" formats the integer num into our string, right justified to three places. Similarly, f"{disp_str:<10}" formats the string disp_str, left-justified to a length of 10

The last thing to note is the use of the argument end="" in print(). The default value of this argument is "\n", which means print adds a newline after whatever we ask it to print. Since we don't want that after each column, we add end="" (which tells python to add nothing after the argument to print). After all columns are done, we print nothing, but let it add the default newline, which takes us onto a new line in the terminal.

n_cols = 4
n_rows = 30

for row in range(n_rows):
    for col in range(n_cols):
        num = row   n_rows * col       # Find which character goes in this row/col
        char = chr(num)                # Get the ascii character
        if not char.isprintable(): 
            char = "X"                 # If it is not printable, replace it with a glyph for display
        disp_str = f"{num:>3}. {char}"      # Format num and char into a string
        print(f"{disp_str:<10}", end="") # Left-justify disp_str to 10 places. Do not print the default newline

    print("")   # After the row is done, we can print nothing (plus the default newline)

This gives the output:

  0. X     30. X     60. <     90. Z    
  1. X     31. X     61. =     91. [    
  2. X     32.       62. >     92. \    
  3. X     33. !     63. ?     93. ]    
  4. X     34. "     64. @     94. ^    
  5. X     35. #     65. A     95. _    
  6. X     36. $     66. B     96. `    
  7. X     37. %     67. C     97. a    
  8. X     38. &     68. D     98. b    
  9. X     39. '     69. E     99. c    
 10. X     40. (     70. F    100. d    
 11. X     41. )     71. G    101. e    
 12. X     42. *     72. H    102. f    
...

CodePudding user response:

To print multiple characters on the same line you will need to either print multiple in one statement or alternatively change the default end "\n" to prevent the newline after each print statement.

Then you can perform a check to print a newline only after 4 characters have been printed.

The below code prints the characters left to right, and then down.

for i in range(1,121):
    a = chr(i)
    print(str(i) ". " str(a), end=" ")

    if i % 4 == 0:
        print()

I believe you may see some formatting issues due to printing some ASCII characters, however.

  • Related