join in Python
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

join in Python

Publish Date: Aug 4
0 0

Buy Me a Coffee

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

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

str.join() and bytes.join() or bytearray.join() can concatenate the zero or more characters of a string and byte string respectively with the zero or more characters of each string and byte string respectively of an iterable as shown below:

*Memos

  • The 1st argument is iterable(Required-Type:iterable(str for str.join() or bytes-like object for bytes.join() and bytearray.join())). *Don't use iterable=.

<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
Enter fullscreen mode Exit fullscreen mode
v = ['', '', '', '']

print(''.join(v))
print(' '.join(v))
print('  '.join(v))
# Nothing

print('-'.join(v))
# ---

print(' - '.join(v))
#  -  -  - 

print('<->'.join(v))
# <-><-><->
Enter fullscreen mode Exit fullscreen mode
v = []

print('-'.join(v))
# Nothing
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

<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'
Enter fullscreen mode Exit fullscreen mode
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'<-><-><->'
Enter fullscreen mode Exit fullscreen mode
v = []

print(b'-'.join(v))
# b''
Enter fullscreen mode Exit fullscreen mode
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'
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
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'<-><-><->')
Enter fullscreen mode Exit fullscreen mode
v = []

print(bytearray(b'-').join(v))
# bytearray(b'')
Enter fullscreen mode Exit fullscreen mode
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')
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment