Join and split functions in Python

Source: Internet
Author: User
Tags iterable

One is the split, the other is the connection.

Convention, see the internal help document first

Help on Method_descriptor:join (...)    S.join (iterable), String        Return a string which is the concatenation of the strings in the Iterable    .  The separator between elements is S. (END)

The iterated object (which should contain the STR type or error) is concatenated, and the return value is str, as follows:

In [2]: s = [' Hello ', ' World ']in [3]: ' _ '. Join (s)                                                                                                                                                     out[3]: ' Hello_world ' in [4]: ". Join (s) out[4]: ' HelloWorld ' in [5]: ' && '. Join (s) out[5]: ' Hello&&world ' in [6]: ' _ '. Join ((1, 2, 3))                                                                                                                                             ---------------------------------- -----------------------------------------TypeError                                 Traceback (most recent) < Ipython-input-6-48e56abfc814> in <module> ()----> 1 ' _ '. Join ((1, 2, 3) typeerror:sequence Item 0:expected St ring, int found

Look again at the Split function:

Help on Method_descriptor:split (...)    S.split ([Sep [, Maxsplit]]), List of strings        Return A list of the words in the string S, using Sep as the    Deli Miter string.  If Maxsplit is given, at most maxsplit    splits be done. If Sep is no specified or is None, any    whitespace string is a separator and empty strings be removed from the    re Sult. (END)

The string is split, the space or null character is removed, the return value is a list of STR, the second parameter is the number of splits, and is used as follows:

In [9]: s = ' life was short, I use Python ' In [ten]: S.spls.split s.splitlines in [ten]: S.split () out[10]: [' Life ' , ' is ', ' short, ', ' I ', ' use ', ' Python ']in [all]: S.split (', ') out[11]: [' Life was short ', ' I use Python ']in []: S.split (', ' or '. ')                                                                                                                                                OUT[12]: [' Life was short ', ' I use Python ']in []: S.split (', ', 3) OUT[13]: [' Life was short ', ' I us                                                                                                                                                e Python ']in []: S.split (', ', 5) OUT[14]: [' Life was short ', ' I use Python ']in []: s = ' Hello, wo Rld.                                                                                                        Life was short, I use Python '        in [+]: s.split (', ', 5) out[16]: [' Hello ', ' world. Life was short ', ' I use Python ']in [+]: s.split (', ', 2) out[17]: [' Hello ', ' world. Life was short ', ' I use Python ']in [+]: s.split (', ', 1) out[18]: [' Hello ', ' world. Life was short, I use Python ']in [+]: s.split (', ' or '. ', 2) out[19]: [' Hello ', ' world. Life was short ', ' I use Python ']

  

  

 

  

Join and split functions in Python

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.