*Memo for string, bytes and bytearray functions:
- My post explains center().
- My post explains ljust().
- My post explains zfill().
- My post explains expandtabs().
- My post explains encode() and decode().
*Memo for a string, bytes and bytearray:
str.rjust() and bytes.rjust() or bytearray.rjust() can right-justify the string and bytes or bytearray set width
respectively as shown below:
*Memo:
- The 1st argument is
width
(Required-Type:int
):- It's the width of the string and bytes or bytearray respectively.
- Padding is added to the left side of the string and bytes or bytearray if
width
is longer than the length of the original string and bytes or bytearray respectively. - Don't use
width=
.
- The 2nd argument is
fillbyte
(Optional-Defualt:" "
-Type:str
forstr.rjust()
orbytes
/bytearray
forbytes.rjust()
andbytearray.rjust()
):- It's the character and byte to fill the padding of the string and bytes or bytearray set
width
respectively. - It must be one character and byte respectively.
- Don't use
fillbyte=
.
- It's the character and byte to fill the padding of the string and bytes or bytearray set
<String>:
v = "John Smith"
print('"' + v.rjust(20) + '"')
print('"' + v.rjust(20, " ") + '"')
# " John Smith"
# ↑↑↑↑↑↑↑↑↑↑
print('"' + v.rjust(20, "?") + '"')
# "??????????John Smith"
print('"' + v.rjust(17, "?") + '"')
# "???????John Smith"
print('"' + v.rjust(14, "?") + '"')
# "????John Smith"
print('"' + v.rjust(13, "?") + '"')
# "???John Smith"
print('"' + v.rjust(12, "?") + '"')
# "??John Smith"
print('"' + v.rjust(11, "?") + '"')
# "?John Smith"
print('"' + v.rjust(10, "?") + '"')
print('"' + v.rjust(0, "?") + '"')
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print('"' + v.rjust(0, "?") + '"')
# " John Smith "
# ↑↑↑ ↑↑↑
print('"' + v.rjust(20, "?") + '"')
# "???? John Smith "
# ↑↑↑ ↑↑↑
<Bytes & Bytearray>:
bytes:
v = "John Smith".encode()
v = b"John Smith"
print(v.rjust(20))
print(v.rjust(20, " ".encode()))
print(v.rjust(20, b" "))
print(v.rjust(20, bytearray(" ".encode())))
# b' John Smith'
# ↑↑↑↑↑↑↑↑↑↑
print(v.rjust(20, "?".encode()))
print(v.rjust(20, b"?"))
print(v.rjust(20, bytearray("?".encode())))
# b'??????????John Smith'
print(v.rjust(17, "?".encode()))
print(v.rjust(17, b"?"))
print(v.rjust(17, bytearray("?".encode())))
# b'???????John Smith'
print(v.rjust(14, "?".encode()))
print(v.rjust(14, b"?"))
print(v.rjust(14, bytearray("?".encode())))
# b'????John Smith'
print(v.rjust(13, "?".encode()))
print(v.rjust(13, b"?"))
print(v.rjust(13, bytearray("?".encode())))
# b'???John Smith'
print(v.rjust(12, "?".encode()))
print(v.rjust(12, b"?"))
print(v.rjust(12, bytearray("?".encode())))
# b'??John Smith'
print(v.rjust(11, "?".encode()))
print(v.rjust(11, b"?"))
print(v.rjust(11, bytearray("?".encode())))
# b'?John Smith'
print(v.rjust(10, "?".encode()))
print(v.rjust(10, b"?"))
print(v.rjust(10, bytearray("?".encode())))
print(v.rjust(0, "?".encode()))
print(v.rjust(0, b"?"))
print(v.rjust(0, bytearray("?".encode())))
# b'John Smith'
v = "John Smith ".encode()
v = b'John Smith '
# ↑↑↑
print(v.rjust(0, "?".encode()))
print(v.rjust(0, b"?"))
print(v.rjust(0, bytearray("?".encode())))
# b'John Smith '
# ↑↑↑
print(v.rjust(20, "?".encode()))
print(v.rjust(20, b"?"))
print(v.rjust(20, bytearray("?".encode())))
# b'???????John Smith '
# ↑↑↑
bytearray:
v = bytearray("John Smith".encode())
print(v.rjust(20))
print(v.rjust(20, " ".encode()))
print(v.rjust(20, b" "))
print(v.rjust(20, bytearray(" ".encode())))
# bytearray(b' John Smith')
# ↑↑↑↑↑↑↑↑↑↑
print(v.rjust(20, "?".encode()))
print(v.rjust(20, b"?"))
print(v.rjust(20, bytearray("?".encode())))
# bytearray(b'??????????John Smith')
print(v.rjust(17, "?".encode()))
print(v.rjust(17, b"?"))
print(v.rjust(17, bytearray("?".encode())))
# bytearray(b'???????John Smith')
print(v.rjust(14, "?".encode()))
print(v.rjust(14, b"?"))
print(v.rjust(14, bytearray("?".encode())))
# bytearray(b'????John Smith')
print(v.rjust(13, "?".encode()))
print(v.rjust(13, b"?"))
print(v.rjust(13, bytearray("?".encode())))
# bytearray(b'???John Smith')
print(v.rjust(12, "?".encode()))
print(v.rjust(12, b"?"))
print(v.rjust(12, bytearray("?".encode())))
# bytearray(b'??John Smith')
print(v.rjust(11, "?".encode()))
print(v.rjust(11, b"?"))
print(v.rjust(11, bytearray("?".encode())))
# bytearray(b'?John Smith')
print(v.rjust(10, "?".encode()))
print(v.rjust(10, b"?"))
print(v.rjust(10, bytearray("?".encode())))
print(v.rjust(0, "?".encode()))
print(v.rjust(0, b"?"))
print(v.rjust(0, bytearray("?".encode())))
# bytearray(b'John Smith')
v = bytearray(" John Smith ".encode())
# ↑↑↑ ↑↑↑
print(v.rjust(0, "?".encode()))
print(v.rjust(0, b"?"))
print(v.rjust(0, bytearray("?".encode())))
# bytearray(b' John Smith ')
# ↑↑↑ ↑↑↑
print(v.rjust(20, "?".encode()))
print(v.rjust(20, b"?"))
print(v.rjust(20, bytearray("?".encode())))
# bytearray(b'???? John Smith ')
# ↑↑↑ ↑↑↑
Top comments (0)