Usage and examples of python's join function

Table of contents

1. Syntax and usage of join function

(1) Syntax: 'sep'.join(sep_object)

(2) Usage: Concatenate any number of strings (including element strings, tuples, lists, dictionaries to be concatenated), concatenate with a new target delimiter, and return a new string.

2. Instance of join function

(1) The object is a string

(2) The object is a tuple

(3) The object is a list

(4) The object is a dictionary

3. Mistakes and solutions

(1) Problem: Errors are reported when storing non-string tuples, lists, dictionaries, etc., such as tuples storing numbers for connection

 (2) Solution: To connect the numbers into a string, combine the for loop statement and convert the numbers into strings and then connect them


1. Syntax and usage of join function

(1) Syntax: 'sep'.join(sep_object)

Parameter Description

sep: Separator, which can be ",,;" and so on.

sep_object: Separation objects, which can be strings, and tuples, lists, and dictionaries that store strings.

(2) Usage: Concatenate any number of strings (including element strings, tuples, lists, dictionaries to be concatenated), concatenate with a new target delimiter, and return a new string.


2. Instance of join function

(1) The object is a string

';'.join('abc') #输出结果为:'a;b;c'  

string1 = 'good idea'#定义字符串
' '.join(string1)  输出结果:'g o o d   i d e a' 

#说明:由于字符串里没指明按字符串之间是怎么连接的,默认每个字符之间插入目标字符

The running result is shown in the figure below.

(2) The object is a tuple

tuple1 = ('a','b','c')  #定义元组tuple1
'、'.join(tuple1)

tuple2 = ('hello','peace','world')  #定义元组tuple2
' '.join(tuple2)

 The result of running jupyter is shown in the figure below.

 (3) The object is a list

b = ['a','b','c'] #定义列表
'、'.join(b)

list1 = ['hello','peace','world'] #定义列表
' '.join(list1)

The running result is shown in the figure below

(4) The object is a dictionary

c={'hello':1,'world':2}
';'.join(c)
d = {'hello':'hi','world':'2'}
' '.join(d)

The output result is shown in the figure below


3. Mistakes and solutions

(1) Problem: Errors are reported when storing non-string tuples, lists, dictionaries, etc., such as tuples storing numbers for connection

a = (1,2,3) #定义元组a
';'.join(a)
#报错:TypeError: sequence item 0: expected str instance, int found

The error is shown in the figure below

 (2) Solution: To connect the numbers into a string, combine the for loop statement and convert the numbers into strings and then connect them

a = (1,2,3) #定义元组a
';'.join(a)

b = (186234,1385475,1235462)
';'.join(str(i) for i in b)

#调用set函数去重处理
e = (1,2,3,2) #定义元组a
'、'.join(str(i) for i in set(e))

Note: The object stored in the split object must be a string, otherwise an error will be reported

Reference article: Detailed explanation of the usage of the join() function in Python

Python common string, tuple, list syntax and examples can refer to python common four data types (number, string, tuple, list) syntax and examples

For the usage of python's set function, please refer to the usage of python's set function - to obtain a non-repeated collection - Blog - CSDN Blog

Python's dictionary dictionary can refer to: https://blog.csdn.net/weixin_50853979/article/details/125122028

Guess you like

Origin blog.csdn.net/weixin_50853979/article/details/125119368