Explain how to use the join () function in Python

Source: Internet
Author: User
This article describes how to use the join () function in Python. the join () function is mainly used to concatenate strings. it is the basic knowledge in Python learning. For more information, see the following functions: string. join ()

Python has two functions: join () and OS. path. join (). The specific functions are as follows:
Join (): concatenate a string array. Concatenates string, tuples, and list elements with specified characters (delimiters) to generate a new string.
OS. path. join (): returns the result after combining multiple paths.

I. function description
1. join () function

Syntax: 'sept'. join (seq)

Parameter description
Sep: delimiter. Can be blank
Seq: sequence, string, tuples, and dictionary of elements to be connected
The preceding syntax uses sep as the separator to merge all seq elements into a new string.

Return value: returns a string generated when the separator sep is used to connect each element.

2. OS. path. join () function

Syntax: OS. path. join (path1 [, path2 [,...])

Return value: returns the result after combining multiple paths.

Note: parameters before the first absolute path will be ignored.

II. instances

# Perform operations on the sequence (use ''and ':' as the separator respectively) >>> seq1 = ['hello', 'good', 'Boys ', 'doiid']> print ''. join (seq1) hello good boy doiido >>> print ':'. join (seq1) hello: good: boy: doiido # operate the string >>> seq2 = "hello good boy doiido" >>> print ':'. join (seq2) h: e: l: o: g: o: d: B: o: y: d: o: I: d: o # perform operations on tuples >>> seq3 = ('hello', 'good', 'Boys', 'doiid') >>> print ':'. join (seq3) hello: good: boy: doiido # operate the dictionary >>> seq4 = {'hello': 1, 'good': 2, 'Boys': 3, 'doiid': 4 }>> print ':'. join (seq4) boy: good: doiido: hello # merged directory >>> import OS >>> OS. path. join ('/hello/', 'good/boy/', 'doiid')'/hello/good/boy/doiido'

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.