Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.ljust

bytearray — padding

Left-justify in a field of the given width.

Syntax

ba.ljust(...)

Even though bytearray is mutable, this method changes nothing in place — it returns a new object that you have to assign. The fill must be a bytes-like object exactly one byte long (b'.', not '.' and not b'..'), otherwise you get a TypeError. The argument is the total field width, not the number of bytes added, and longer input is returned unchanged rather than truncated.

Examples

print(bytearray(b'hi').ljust(5, b'.'))   # → bytearray(b'hi...')
print(bytearray(b'hi').ljust(5))   # → bytearray(b'hi   ')
print(bytearray(b'7').ljust(4, b'0'))   # → bytearray(b'7000')
print(bytearray(b'id').ljust(6, b'.') + bytearray(b'42'))   # → bytearray(b'id....42')
print(bytearray(b'hello').ljust(3))   # → bytearray(b'hello')
print(bytearray(b'hi').rjust(5, b'.'))   # → bytearray(b'...hi')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy