DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

join in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a 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 bytes or bytearray with the zero or more characters and bytes of each string and bytes or bytearray respectively of an iterable as shown below:

*Memo:

  • 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

<Bytes & Bytearray>:

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

Top comments (0)