*Memo for string, bytes and bytearray functions:
- My post explains ljust().
- My post explains rjust().
- My post explains zfill().
- My post explains expandtabs().
*Memo for a string, bytes and bytearray:
str.center() and bytes.center() or bytearray.center() can center 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 and/or right side of the string and bytes or bytearray if
width
is longer than the length of the original string and bytes or bytearray respectively. *From which side of the string and bytes or bytearray padding starts to be added is irregular ifwidth
is oddly 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.center()
orbytes
/bytearray
forbytes.center()
andbytearray.center()
):- 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.center(20) + '"')
print('"' + v.center(20, " ") + '"')
# " John Smith "
# ↑↑↑↑↑ ↑↑↑↑↑
print('"' + v.center(20, "?") + '"')
# "?????John Smith?????"
print('"' + v.center(17, "?") + '"')
# "????John Smith???"
print('"' + v.center(14, "?") + '"')
# "??John Smith??"
print('"' + v.center(13, "?") + '"')
# "??John Smith?"
print('"' + v.center(12, "?") + '"')
# "?John Smith?"
print('"' + v.center(11, "?") + '"')
# "?John Smith"
print('"' + v.center(10, "?") + '"')
print('"' + v.center(0, "?") + '"')
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print('"' + v.center(0, "?") + '"')
# " John Smith "
# ↑↑↑ ↑↑↑
print('"' + v.center(20, "?") + '"')
# "?? John Smith ??"
# ↑↑↑ ↑↑↑
<Bytes & Bytearray>:
bytes:
v = b"John Smith"
print(v.center(20))
print(v.center(20, b" "))
print(v.center(20, bytearray(b" ")))
# b' John Smith '
# ↑↑↑↑↑ ↑↑↑↑↑
print(v.center(20, b"?"))
print(v.center(20, bytearray(b"?")))
# b'?????John Smith?????'
print(v.center(17, b"?"))
print(v.center(17, bytearray(b"?")))
# b'????John Smith???'
print(v.center(14, b"?"))
print(v.center(14, bytearray(b"?")))
# b'??John Smith??'
print(v.center(13, b"?"))
print(v.center(13, bytearray(b"?")))
# b'??John Smith?'
print(v.center(12, b"?"))
print(v.center(12, bytearray(b"?")))
# b'?John Smith?'
print(v.center(11, b"?"))
print(v.center(11, bytearray(b"?")))
# b'?John Smith'
print(v.center(10, b"?"))
print(v.center(10, bytearray(b"?")))
print(v.center(0, b"?"))
print(v.center(0, bytearray(b"?")))
# b'John Smith'
v = b" John Smith "
# ↑↑↑ ↑↑↑
print(v.center(0, b"?"))
print(v.center(0, bytearray(b"?")))
# b' John Smith '
# ↑↑↑ ↑↑↑
print(v.center(20, b"?"))
print(v.center(20, bytearray(b"?")))
# b'?? John Smith ??'
# ↑↑↑ ↑↑↑
bytearray:
v = bytearray(b"John Smith")
print(v.center(20))
print(v.center(20, b" "))
print(v.center(20, bytearray(b" ")))
# bytearray(b' John Smith ')
# ↑↑↑↑↑ ↑↑↑↑↑
print(v.center(20, b"?"))
print(v.center(20, bytearray(b"?")))
# bytearray(b'?????John Smith?????')
print(v.center(17, b"?"))
print(v.center(17, bytearray(b"?")))
# bytearray(b'????John Smith???')
print(v.center(14, b"?"))
print(v.center(14, bytearray(b"?")))
# bytearray(b'??John Smith??')
print(v.center(13, b"?"))
print(v.center(13, bytearray(b"?")))
# bytearray(b'??John Smith?')
print(v.center(12, b"?"))
print(v.center(12, bytearray(b"?")))
# bytearray(b'?John Smith?')
print(v.center(11, b"?"))
print(v.center(11, bytearray(b"?")))
# bytearray(b'?John Smith')
print(v.center(10, b"?"))
print(v.center(10, bytearray(b"?")))
print(v.center(0, b"?"))
print(v.center(0, bytearray("?".encode())))
# bytearray(b'John Smith')
v = bytearray(b" John Smith ")
# ↑↑↑ ↑↑↑
print(v.center(0, b"?"))
print(v.center(0, bytearray(b"?")))
# bytearray(b' John Smith ')
# ↑↑↑ ↑↑↑
print(v.center(20, b"?"))
print(v.center(20, bytearray(b"?")))
# bytearray(b'?? John Smith ??')
# ↑↑↑ ↑↑↑
Top comments (0)