Analysis of join and split in Python

Source: Internet
Author: User
This article mainly introduces the details about join and split in Python. This article also uses an example to introduce the use of pythonjoin and split methods, for more information, see The python join and split methods. join is used to connect strings. split is the opposite.

. Join ()

Join splits the container object and connects the elements in the list with specified characters. return a string (note: the elements in the container object must be of the character type)

 >>> a = ['no','pain','no','gain']  >>> '_ '.join(a)  'no_pain_no_gain'  >>>

Note: the elements in the container object must be of the character type.

>>> b = ['I','am','no',1]  >>> '_'.join(b)  Traceback (most recent call last):  File "
 
  ", line 1, in 
  
     '_'.join(b)  TypeError: sequence item 3: expected string, int found  >>>
  
 

Dict is connected with Key values

>>> L = {'P': 'P', 'y': 'y', 'T': 't', 'H': 'h ', 'O': 'O', 'n': 'N' }>>> '_'. join (L) 'H _ o_n_p_t_y '# The disorder of dict, so that the elements are connected randomly. Set likewise >>>

. Split ()

In contrast to join, split splits a string into a single element (character type) and adds it to the list. a List is returned.

>>> A = 'no _ pian_no_gain '>. split ('_') ['no', 'piany', 'no ', 'gain'] >>> split indicates the number of characters to be cut. >>> a = 'no _ pian_no_gain '>>>. split ('_', 2) ['no', 'piany', 'no _ gain']>. split ('_', 1) ['no', 'pian _ no_gain']>. split ('_', 0) ['no _ pian_no_gain ']>. split ('_',-1) ['no', 'piany', 'no', 'gain']>

It can be seen that the results returned by split ('_') and split ('_',-1) are consistent.

The following example shows how to use python join and split.

1. join usage example

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

2. example of split usage

>>> b = 'my..name..is..bob' >>> b.split() ['my..name..is..bob'] >>> b.split("..") ['my', 'name', 'is', 'bob'] >>> b.split("..",0) ['my..name..is..bob'] >>> b.split("..",1) ['my', 'name..is..bob'] >>> b.split("..",2) ['my', 'name', 'is..bob'] >>> b.split("..",-1) ['my', 'name', 'is', 'bob']

We can see that B. split ("...",-1) is equivalent to B. split ("..")

For more analysis on the article about join and split in Python, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.