The use of the Join () function in Python

Source: Internet
Author: User

Function: String.Join ()

Python has join () and Os.path.join () two functions, which work as follows:

Join (): An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Os.path.join (): Combine multiple paths to return

First, function description

1. Join () function

Syntax: ' Sep '. Join (SEQ)

Parameter description
Sep: Delimiter. Can be empty
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries


The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter

Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep

2. Os.path.join () function

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

Return value: Combine multiple paths to return

Note: Parameters before the first absolute path are ignored

Second, examples

1. Manipulate the sequence (with '. ') As a delimiter)

seq = [' Hello ', ' good ', ' boy ', ' Doiido ']

Print ('. '). Join (SEQ))

Hello.good.boy.doiido

2. Operations on tuples (with ': ' as delimiters)

Seq = (' hello ', ' good ', ' boy ', ' Doiido ')

Print (': '. Join (SEQ))

Hello:good:boy:doiido

3. Working with Dictionaries

Seq = {' Hello ': 1, ' good ': 2, ' Boy ': 3, ' Doiido ': 4}

Print (': '. Join (SEQ))

Doiido:boy:hello:good

4. Merging catalogs

Import OS

Print (Os.path.join ('/hello/', ' good/boy/', ' Doiido '))

/hello/good/boy/doiido

Three. JSON dictionary to SQL statement

#表名polls_questions

table_name = "Polls_questions"

#需要插入的Json数据
data={' id ': 1, ' question_text ': ' You buy Pro6? ', ' pub_date ': ' 2016-07-23 09:58:56.000000 '}

#对每一个值加单引号
For key in data:
Data[key] = "'" + str (Data[key]) + "'"

The #利用join () function merges IDs, Question_text, pub_date together (id,question_text,pub_date)
Key = ', '. Join (Data.keys ())


#利用join () function merges the values together (' 1 ', ' You buy Pro6? ', ' 2016-07-23 09:58:56.000000 ')
Value = ', '. Join (Data.values ())

#INSERT into Polls_questions (id,pub_date,question_text) VALUES (' 1 ', ' 2016-07-23 09:58:56.000000 ', ' buy Pro6? ')

Real_sql = "INSERT into" + table_name + "(" + key + ") VALUES (" + Value + ")"

The use of the Join () function in Python

Related Article

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.