Right-justify, Center, Left-justify Strings and Numbers in Python
Use the rjust()
, center()
, or ljust()
methods to right-justify, center, or left-justify strings str
in Python. You can convert numbers (int
and float
) to strings using str()
and apply these methods.
Right-justify strings: rjust()
Use the rjust()
method to right-justify strings.
The first argument determines the length of the output string. By default, it is filled with spaces.
s = 'abc'
print(s.rjust(8))
# abc
print(type(s.rjust(8)))
# <class 'str'>
If a value less or equal to the length of the original string is specified, it returns the original string unchanged.
print(s.rjust(2))
# abc
You can provide a specific padding character as the second argument. However, if you provide a string with more than one character, a TypeError
is raised.
print(s.rjust(8, '*'))
# *****abc
# print(s.rjust(8, 'ab'))
# TypeError: The fill character must be exactly one character long
You can also use the zfill()
method for zero-padding. Unlike rjust()
, zfill()
places zeros after the sign (+
, -
).
s = '-123'
print(s.rjust(8, '0'))
# 0000-123
print(s.zfill(8))
# -0000123
For more details on zero-padding, refer to the following article.
Center strings: center()
Use the center()
method to center strings.
Its usage is the same as rjust()
. If the number of padding characters is odd, the right side will have an extra character.
s = 'abc'
print(s.center(8))
# abc
print(s.center(8, '*'))
# **abc***
print(s.center(9, '*'))
# ***abc***
print(s.center(10, '*'))
# ***abc****
Left-justify strings: ljust()
Use the ljust()
method to left-justify strings.
Its usage is the same as rjust()
and center()
.
Although it may not be visible in the output, trailing spaces are added when the second argument is omitted.
s = 'abc'
print(s.ljust(8))
# abc
print(s.ljust(8, '*'))
# abc*****
Apply to numbers (int
and float
)
If you want to right-justify, center, or left-justify a non-string object such as an integer (int
) or floating point number (float
), convert it to a string with str()
and call these methods.
i = 123
print(type(i))
# <class 'int'>
print(str(i).rjust(8, '*'))
# *****123
print(str(i).center(8, '*'))
# **123***
print(str(i).ljust(8, '*'))
# 123*****
Format strings: format()
, f-strings
Both the built-in format()
function and the format()
method of str
allow various formatting options. See the following article for more information.
To right-justify, center, or left-justify, use [CHARACTER][DIRECTION][STRING_LENGTH]
as the format string. Use >
, ^
, or <
to indicate the direction.
s = 'abc'
print('right : {:*>8}'.format(s))
# right : *****abc
print('center: {:*^8}'.format(s))
# center: **abc***
print('left : {:*<8}'.format(s))
# left : abc*****
Numbers can be directly formatted as well. You can perform various operations, such as converting to hexadecimal or adding zero-padding. In the case of zero-padding, it will be right-justified if the direction is omitted.
i = 255
print('right : {:08}'.format(i))
# right : 00000255
print('right : {:08x}'.format(i))
# right : 000000ff
In Python 3.6 and later, you can use f-strings for more concise formatting.
print(f'right : {i:08}')
# right : 00000255
print(f'right : {i:08x}')
# right : 000000ff