String join, strip, split analysis

1. Join usage: connect the list to a string according to the custom method

l = ['a','a','a','a']

L1 = ''.join(l)   #output is 'aaaa'

L2 = 'b'.join(l) #output is 'abababa'

L3 = '5'.join(l)  # output is 'a5a5a5a'

 

2. Split usage: split the string into a list in a custom way (restored from Example 1)

L2.split('b')   #result is ['a', 'a', 'a', 'a']

L3.split('5')   #result is ['a', 'a', 'a', 'a']

3. Strip usage: remove the characters at both ends of the character (the default is a space, of course, it can also be placed in parentheses with custom characters), so there are rstrip(), lstrip(), which means to remove the right side and remove the left side

l = '222hello22222222'

l.strip('2')   #output is 'hello'

l.rstrip('2')  # output is 222hello

l.lstrip('2')  #output is hello2222222

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325311530&siteId=291194637