Python: String Formatter Align center [duplicate]

Solution 1:

Use the new-style format method instead of the old-style % operator, which doesn't have the centering functionality:

print('{:^24s}'.format("MyString"))

Solution 2:

You can use str.center() method.

In your case, it will be: "MyString".center(24)