DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

ljust in Python

Buy Me a Coffee

*Memo for a string and byte string(bytes and bytearray) functions:

*Memo for a string and byte string(bytes and bytearray):

str.ljust() and bytes.ljust() or bytearray.ljust() can left-justify(left-align) the string and byte string set width respectively as shown below:

*Memo:

  • The 1st argument is width(Required-Type:int):
    • It decides the width of the string and byte string respectively.
    • Padding is added to the right side of the string and byte string if width is longer than the length of the original string and byte string respectively.
    • Don't use width=.
  • The 2nd argument is fillbyte(Optional-Defualt:" "-Type:str for str.ljust() or bytes/bytearray for bytes.ljust() and bytearray.ljust()):
    • It's the character and byte to fill the padding of the string or byte string set width respectively.
    • It must be one character and byte respectively.
    • Don't use fillbyte=.

<String>:

v = "John Smith"

print('"' + v.ljust(20) + '"')
print('"' + v.ljust(20, " ") + '"')
# "John Smith          "
#            ↑↑↑↑↑↑↑↑↑↑

print('"' + v.ljust(20, "?") + '"')
# "John Smith??????????"

print('"' + v.ljust(17, "?") + '"')
# "John Smith???????"

print('"' + v.ljust(14, "?") + '"')
# "John Smith????"

print('"' + v.ljust(13, "?") + '"')
# "John Smith???"

print('"' + v.ljust(12, "?") + '"')
# "John Smith??"

print('"' + v.ljust(11, "?") + '"')
# "John Smith?"

print('"' + v.ljust(10, "?") + '"')
print('"' + v.ljust(0, "?") + '"')
# "John Smith"
Enter fullscreen mode Exit fullscreen mode
v = "   John Smith   "
   # ↑↑↑          ↑↑↑

print('"' + v.ljust(0, "?") + '"')
# "   John Smith   "
#  ↑↑↑          ↑↑↑

print('"' + v.ljust(20, "?") + '"')
# "   John Smith   ????"
#  ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Byte String>:

bytes:

v = b"John Smith"

print(v.ljust(20))
print(v.ljust(20, b" "))
print(v.ljust(20, bytearray(b" ")))
# b'John Smith          '
#             ↑↑↑↑↑↑↑↑↑↑

print(v.ljust(20, b"?"))
print(v.ljust(20, bytearray(b"?")))
# b'John Smith??????????'

print(v.ljust(17, b"?"))
print(v.ljust(17, bytearray(b"?")))
# b'John Smith???????'

print(v.ljust(14, b"?"))
print(v.ljust(14, bytearray(b"?")))
# b'John Smith????'

print(v.ljust(13, b"?"))
print(v.ljust(13, bytearray(b"?")))
# b'John Smith???'

print(v.ljust(12, b"?"))
print(v.ljust(12, bytearray(b"?")))
# b'John Smith??'

print(v.ljust(11, b"?"))
print(v.ljust(11, bytearray(b"?")))
# b'John Smith?'

print(v.ljust(10, b"?"))
print(v.ljust(10, bytearray(b"?")))
print(v.ljust(0, b"?"))
print(v.ljust(0, bytearray(b"?")))
# b'John Smith'
Enter fullscreen mode Exit fullscreen mode
v = b"   John Smith   "
    # ↑↑↑          ↑↑↑

print(v.ljust(0, b"?"))
print(v.ljust(0, bytearray(b"?")))
# b'   John Smith   '
#   ↑↑↑          ↑↑↑

print(v.ljust(20, b"?"))
print(v.ljust(20, bytearray(b"?")))
# b'   John Smith   ????'
#   ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

bytearray:

v = bytearray(b"John Smith")

print(v.ljust(20))
print(v.ljust(20, b" "))
print(v.ljust(20, bytearray(b" ")))
# bytearray(b'John Smith          ')
#                       ↑↑↑↑↑↑↑↑↑↑

print(v.ljust(20, b"?"))
print(v.ljust(20, bytearray(b"?")))
# bytearray(b'John Smith??????????')

print(v.ljust(17, b"?"))
print(v.ljust(17, bytearray(b"?")))
# bytearray(b'John Smith???????')

print(v.ljust(14, b"?"))
print(v.ljust(14, bytearray(b"?")))
# bytearray(b'John Smith????')

print(v.ljust(13, b"?"))
print(v.ljust(13, bytearray(b"?")))
# bytearray(b'John Smith???')

print(v.ljust(12, b"?"))
print(v.ljust(12, bytearray(b"?")))
# bytearray(b'John Smith??')

print(v.ljust(11, b"?"))
print(v.ljust(11, bytearray(b"?")))
# bytearray(b'John Smith?')

print(v.ljust(10, b"?"))
print(v.ljust(10, bytearray(b"?")))
print(v.ljust(0, b"?"))
print(v.ljust(0, bytearray(b"?")))
# bytearray(b'John Smith')
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b"   John Smith   ")
              # ↑↑↑          ↑↑↑

print(v.ljust(0, b"?"))
print(v.ljust(0, bytearray(b"?")))
# bytearray(b'   John Smith   ')
#             ↑↑↑          ↑↑↑

print(v.ljust(20, b"?"))
print(v.ljust(20, bytearray(b"?")))
# bytearray(b'   John Smith   ????')
#             ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)