Python-Join ()

Source: Internet
Author: User

Python-Join () Pythonjoinos


The moon seems to be at that time.

Total

In Python, there are two join methods, one is Str.join () and the other is Os.path.join (), which only understands the previous

Str.join (iterable)

Official documents
Return a string which is the concatenation of the strings in iterable. A TypeError'll be raised if there is any non-string the values in Iterable, including bytes objects. The separator between elements is the string providing this method.

To put it simply, the elements in an iterative object are stitched back together in Str, but these elements must be of type String, or the error will be

< Code data-info= "" class= "Nohighlight hljs" style= "border:0; font-size:90%; Background-color: #FFCCCC; Color: #f8f8f2; Padding-top:0.5em; Padding-right:0.5em; Padding-bottom:0.5em; Padding-left:0.5em; Overflow:visible!important; Display:block; Background: #282a36 "># error >>> L = [1,2,3]>>>" ". Join (L) Traceback (most recent call last): File" <std In> ", line 1, in <module>typeerror:sequence item 0:expected str instance, int found# to list stitching >>> L = ["Hello", "pinsily"]>>> "". Join (L) ' Hello pinsily ' # to string concatenation >>> s = "Hello pinsily" >>> ":". J Oin (s) ' H:e:l:l:o::p: I:n:s:i:l:y ' # for tuple stitching >>> t = ("Hello", "pinsily") >>> "". Join (t) ' Hello pinsily ' # To the dictionary stitching >>> d = {"Hello": "1", "pinsily": "2", "and": "3", "World": "4"}>>> "". Join (d) ' Hello pinsily and World '  

Note : When you want to use a lot of string stitching, try to avoid + the operation, which will produce a lot of temporary variables, occupy memory, you can first stitching it into the list, and then use the join method

Python-Join ()

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.