TEXT ALIGNMENT
Python provides the methods ljust(), rjust(), and center() for aligning text. The ljust() and rjust() functions left justify and right justify a text string, respectively, whereas the center() function will center a string. An example is shown in the following code block:
text = 'Hello World' text.ljust(20) 'Hello World ' >>> text.rjust(20) ' Hello World' >>> text.center(20) ' Hello World '
You can use the format() function to align text. Use the <, >, or ^ characters, along with a desired width, in order to right justify, left justify, and center the text, respectively. The following examples illustrate how you can specify text justification:
>>> format(text, '>20') ' Hello World' >>> >>> format(text, '<20') 'Hello World ' >>> >>> format(text, '^20') ' Hello World ' >>>