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'