Search code examples
pythonstringpython-3.xformattingstring-formatting

Print right aligned table column involving English and Japanese text in Python3


I have a column consisting of English and Japanese characters and I need to print the column in right justified manner.

This is the column I am supposed to print :

column = ["通常残業時間", "bbbbbbbbb", "tttt"]

Normal way would be to get maximum length of string and adjust accordingly but the problem is that text is in japanese as well and width of a japanese character is more than that of an english one. How should I compare the string lengths in this case and print accordingly?

This is the required output :

通常残業時間
 bbbbbbbbb
      tttt

I am working in Python3.


Solution

  • You can use r.just on the last two items of column

    column = ["通常残業時間", "bbbbbbbbb", "tttt"]
    
    for idx, item in enumerate(column):
        if not idx:
            print(item)
        else:
            print(item.rjust(12))
    
    通常残業時間
     bbbbbbbbb
          tttt