*Memo for a string and byte string(bytes
and bytearray
) functions:
- My post explains split().
- My post explains rsplit().
- My post explains splitlines().
- My post explains partition().
- My post explains rpartition().
*Memo for a string and byte string(bytes
and bytearray
):
str.join() and bytes.join() or bytearray.join() can concatenate the zero or more characters and bytes of the string and byte string respectively with the zero or more characters and bytes of each string and byte string respectively of an iterable as shown below:
*Memo:
- The 1st argument is
iterable
(Required-Type:Iterable(str
forstr.join()
or Bytes-like object forbytes.join()
andbytearray.join()
)):- Don't use
iterable=
.
- Don't use
<String>:
v = ['one', 'two', 'three', 'four']
print(''.join(v))
# onetwothreefour
print(' '.join(v))
# one two three four
print(' '.join(v))
# one two three four
print('-'.join(v))
# one-two-three-four
print(' - '.join(v))
# one - two - three - four
print('<->'.join(v))
# one<->two<->three<->four
v = ['', '', '', '']
print(''.join(v))
print(' '.join(v))
print(' '.join(v))
# Nothing
print('-'.join(v))
# ---
print(' - '.join(v))
# - - -
print('<->'.join(v))
# <-><-><->
v = []
print('-'.join(v))
# Nothing
v = [0, 12, 345, 6789]
print(''.join(map(str, v)))
print(''.join([str(num) for num in v]))
# 0123456789
print('-'.join(map(str, v)))
print('-'.join([str(num) for num in v]))
# 0-12-345-6789
<Byte String>:
bytes:
v = [b'one', b'two', b'three', b'four']
v = [bytearray(b'one'), bytearray(b'two'),
bytearray(b'three'), bytearray(b'four')]
print(b''.join(v))
# b'onetwothreefour'
print(b' '.join(v))
# b'one two three four'
print(b' '.join(v))
# b'one two three four'
print(b'-'.join(v))
# b'one-two-three-four'
print(b' - '.join(v))
# b'one - two - three - four'
print(b'<->'.join(v))
# b'one<->two<->three<->four'
v = [b'', b'', b'', b'']
v = [bytearray(b''), bytearray(b''), bytearray(b''), bytearray(b'')]
print(b''.join(v))
# b''
print(b' '.join(v))
# b' '
print(b' '.join(v))
# b' '
print(b'-'.join(v))
# b'---'
print(b' - '.join(v))
# b' - - - '
print(b'<->'.join(v))
# b'<-><-><->'
v = []
print(b'-'.join(v))
# b''
v = [2, 4, 6]
print(b''.join(map(bytes, v)))
print(b''.join(map(bytearray, v)))
print(b''.join([bytes(num) for num in v]))
print(b''.join([bytearray(num) for num in v]))
# b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
print(b'-'.join(map(bytes, v)))
print(b'-'.join(map(bytearray, v)))
print(b'-'.join([bytes(num) for num in v]))
print(b'-'.join([bytearray(num) for num in v]))
# b'\x00\x00-\x00\x00\x00\x00-\x00\x00\x00\x00\x00\x00'
bytearray:
v = [b'one', b'two', b'three', b'four']
v = [bytearray(b'one'), bytearray(b'two'),
bytearray(b'three') ,bytearray(b'four')]
print(bytearray(b'').join(v))
# bytearray(b'onetwothreefour')
print(bytearray(b' ').join(v))
# bytearray(b'one two three four')
print(bytearray(b' ').join(v))
# bytearray(b'one two three four')
print(bytearray(b'-').join(v))
# bytearray(b'one-two-three-four')
print(bytearray(b' - ').join(v))
# bytearray(b'one - two - three - four')
print(bytearray(b'<->').join(v))
# bytearray(b'one<->two<->three<->four')
v = [b'', b'', b'', b'']
v = [bytearray(b''), bytearray(b''), bytearray(b''), bytearray(b'')]
print(bytearray(b'').join(v))
# bytearray(b'')
print(bytearray(b' ').join(v))
# bytearray(b' ')
print(bytearray(b' ').join(v))
# bytearray(b' ')
print(bytearray(b'-').join(v))
# bytearray(b'---')
print(bytearray(b' - ').join(v))
# bytearray(b' - - - ')
print(bytearray(b'<->').join(v))
# bytearray(b'<-><-><->')
v = []
print(bytearray(b'-').join(v))
# bytearray(b'')
v = [2, 4, 6]
print(bytearray(b'').join(map(bytes, v)))
print(bytearray(b'').join(map(bytearray, v)))
print(bytearray(b'').join([bytes(num) for num in v]))
print(bytearray(b'').join([bytearray(num) for num in v]))
# bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
print(bytearray(b'-').join(map(bytes, v)))
print(bytearray(b'-').join(map(bytearray, v)))
print(bytearray(b'-').join([bytes(num) for num in v]))
print(bytearray(b'-').join([bytearray(num) for num in v]))
# bytearray(b'\x00\x00-\x00\x00\x00\x00-\x00\x00\x00\x00\x00\x00')
Top comments (0)