*Memo for string, bytes and bytearray functions:
- My post explains center().
- My post explains ljust().
- My post explains rjust().
- My post explains zfill().
*Memo for a string, bytes and bytearray:
str.expandtabs() and bytes.expandtabs() or bytearray.expandtabs() can replace \t
of the string and bytes or bytearray with zero or more spaces respectively as shown below:
*Memo:
- The 1st argument is
tabsize
(Optional-Default:8
-Type:int
):- It's the tab size to replace
\t
with zero or more spaces. - The number of spaces depends on the word before
\t
.
- It's the tab size to replace
<String>:
v = 'We\tlike\tapples.' print(v) # We like apples. # ↑↑ ↑↑↑↑ print(v.expandtabs()) print(v.expandtabs(tabsize=8)) # We like apples. # ↑↑↑↑↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=0)) # Welikeapples. print(v.expandtabs(tabsize=1)) # We like apples. # ↑ ↑ print(v.expandtabs(tabsize=2)) # We like apples. # ↑↑ ↑↑ print(v.expandtabs(tabsize=3)) # We like apples. # ↑ ↑↑ print(v.expandtabs(tabsize=4)) # We like apples. # ↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=5)) # We like apples. # ↑↑↑ ↑ print(v.expandtabs(tabsize=6)) # We like apples. # ↑↑↑↑ ↑↑ print(v.expandtabs(tabsize=7)) # We like apples. # ↑↑↑↑↑ ↑↑↑
v = "12\t1234\t1\t123\n1234\t1\t123\t12" print(v) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs()) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=0)) # 1212341123 # 1234112312 print(v.expandtabs(tabsize=1)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=2)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=3)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=4)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=5)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=6)) # 12 1234 1 123 # 1234 1 123 12 print(v.expandtabs(tabsize=7)) # 12 1234 1 123 # 1234 1 123 12
<Bytes & Bytearray>:
bytes:
v = b'We\tlike\tapples.' print(v) # b'We\tlike\tapples.' print(v.expandtabs()) print(v.expandtabs(tabsize=8)) # b'We like apples.' # ↑↑↑↑↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=0)) # b'Welikeapples.' print(v.expandtabs(tabsize=1)) # b'We like apples.' # ↑ ↑ print(v.expandtabs(tabsize=2)) # b'We like apples.' # ↑↑ ↑↑ print(v.expandtabs(tabsize=3)) # b'We like apples.' # ↑ ↑↑ print(v.expandtabs(tabsize=4)) # b'We like apples.' # ↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=5)) # b'We like apples.' # ↑↑↑ ↑ print(v.expandtabs(tabsize=6)) # b'We like apples.' # ↑↑↑↑ ↑↑ print(v.expandtabs(tabsize=7)) # b'We like apples.' # ↑↑↑↑↑ ↑↑↑
v = b'12\t1234\t1\t123\n1234\t1\t123\t12' print(v) # b'12\t1234\t1\t123\n1234\t1\t123\t12' print(v.expandtabs()) # b'12 1234 1 123\n1234 1 123 12' # ↑↑↑↑↑↑ ↑↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑↑↑ print(v.expandtabs(tabsize=0)) # b'1212341123\n1234112312' print(v.expandtabs(tabsize=1)) # b'12 1234 1 123\n1234 1 123 12' # ↑ ↑ ↑ ↑ ↑ ↑ print(v.expandtabs(tabsize=2)) # b'12 1234 1 123\n1234 1 123 12' # ↑↑ ↑↑ ↑ ↑↑ ↑ ↑ print(v.expandtabs(tabsize=3)) # b'12 1234 1 123\n1234 1 123 12' # ↑ ↑↑ ↑↑ ↑↑ ↑↑ ↑↑↑ print(v.expandtabs(tabsize=4)) # b'12 1234 1 123\n1234 1 123 12' # ↑↑ ↑↑↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑ ↑ print(v.expandtabs(tabsize=5)) # b'12 1234 1 123\n1234 1 123 12' # ↑↑↑ ↑ ↑↑↑↑ ↑ ↑↑↑↑ ↑↑ print(v.expandtabs(tabsize=6)) # b'12 1234 1 123\n1234 1 123 12' # ↑↑↑↑ ↑↑ ↑↑↑↑↑ ↑↑ ↑↑↑↑↑ ↑↑↑ print(v.expandtabs(tabsize=7)) # b'12 1234 1 123\n1234 1 123 12' # ↑↑↑↑↑ ↑↑↑ ↑↑↑↑↑↑ ↑↑↑ ↑↑↑↑↑↑ ↑↑↑↑
bytearray:
v = bytearray(b'We\tlike\tapples.') print(v) # bytearray(b'We\tlike\tapples.') print(v.expandtabs()) print(v.expandtabs(tabsize=8)) # bytearray(b'We like apples.') # ↑↑↑↑↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=0)) # bytearray(b'Welikeapples.') print(v.expandtabs(tabsize=1)) # bytearray(b'We like apples.') # ↑ ↑ print(v.expandtabs(tabsize=2)) # bytearray(b'We like apples.') # ↑↑ ↑↑ print(v.expandtabs(tabsize=3)) # bytearray(b'We like apples.') # ↑ ↑↑ print(v.expandtabs(tabsize=4)) # bytearray(b'We like apples.') # ↑↑ ↑↑↑↑ print(v.expandtabs(tabsize=5)) # bytearray(b'We like apples.') # ↑↑↑ ↑ print(v.expandtabs(tabsize=6)) # bytearray(b'We like apples.') # ↑↑↑↑ ↑↑ print(v.expandtabs(tabsize=7)) # bytearray(b'We like apples.') # ↑↑↑↑↑ ↑↑↑
v = bytearray(b"12\t1234\t1\t123\n1234\t1\t123\t12") print(v) # bytearray(b'12\t1234\t1\t123\n1234\t1\t123\t12') print(v.expandtabs()) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑↑↑↑↑ ↑↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑↑↑ print(v.expandtabs(tabsize=0)) # bytearray(b'1212341123\n1234112312') print(v.expandtabs(tabsize=1)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑ ↑ ↑ ↑ ↑ ↑ print(v.expandtabs(tabsize=2)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑ ↑↑ ↑ ↑↑ ↑ ↑ print(v.expandtabs(tabsize=3)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑ ↑↑ ↑↑ ↑↑ ↑↑ ↑↑↑ print(v.expandtabs(tabsize=4)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑ ↑↑↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑ ↑ print(v.expandtabs(tabsize=5)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑↑ ↑ ↑↑↑↑ ↑ ↑↑↑↑ ↑↑ print(v.expandtabs(tabsize=6)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑↑↑ ↑↑ ↑↑↑↑↑ ↑↑ ↑↑↑↑↑ ↑↑↑ print(v.expandtabs(tabsize=7)) # bytearray(b'12 1234 1 123\n1234 1 123 12') # ↑↑↑↑↑ ↑↑↑ ↑↑↑↑↑↑ ↑↑↑ ↑↑↑↑↑↑ ↑↑↑↑
Top comments (0)