Examples of join and split usage in Python

  • 2020-05-09 18:48:52
  • OfStack

join is used to concatenate strings, split is the opposite, split strings.
No need to explain, after looking at the code, the meaning of its own.


>>>li = ['my','name','is','bob']
>>>' '.join(li)
'my name is bob'
>>>s = '_'.join(li)
>>>s
'my_name_is_bob'
>>>s.split('_')
['my', 'name', 'is', 'bob']

The English versions of join and split are explained as follows:

join(...)
S.join(sequence) - > string

Return a string which is the concatenation of the strings in the
sequence.   The separator between elements is S.

split(...)
S.split([sep [,maxsplit]]) - > list of strings

Return a list of the words in the string S, using sep as the
delimiter string.   If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.


Related articles: