Python string join() method creates a string from an iterable. It joins all the iterable elements with the string as delimiter and returns it.

Python字符串join()方法从可迭代对象创建一个字符串。 它将所有可迭代的元素与字符串作为分隔符相连并返回它。

什么时候使用Python String join()方法? (When to use Python String join() Method?)

Some possible use cases are:

一些可能的用例是:

  • Creating CSV String from an iterable such as List, Tuple, etc.从列表,元组等迭代对象创建CSV字符串。
  • For logging purpose, get the string representation of the iterable and log into file.为了进行日志记录,请获取iterable的字符串表示形式并登录到文件中。
  • Saving an iterable object into a file by converting it to a string.通过将可迭代对象转换为字符串,将其保存到文件中。

join()方法的语法 (Syntax of join() Method)

The join() method syntax is:

join()方法的语法为:

str.join(iterable)

The output is a new string, which we can assign to another variable. We can use List, Tuple, String, and Set as input data types because they are iterables.

输出是一个新字符串,我们可以将其分配给另一个变量。 我们可以使用List,Tuple,String和Set作为输入数据类型,因为它们是可迭代的。

Let’s look at some examples of using string join() method.

让我们看一些使用字符串join()方法的示例。

1.将字符串列表连接到CSV (1. Joining List of Strings to CSV)

delimiter = ","csv_str = delimiter.join(['a', 'b', 'c'])print(csv_str)  # a,b,c

2.字符串的串联 (2. Concatenation of the Strings)

tuple_vowels = ('a', 'e', 'i', 'o', 'u')vowels_str = "".join(tuple_vowels)print(vowels_str)  # aeiou

We can use join() with an empty string to concatenate all the strings in the iterable.

我们可以使用带有空字符串的join()来串联可迭代对象中的所有字符串。

3.将join()与单个字符串一起使用作为输入 (3. Using join() with Single String as input)

str = 'Hello'print(f'String characters are: {",".join(str)}')

Output:

输出:

String characters are: H,e,l,l,o

The string is iterable in Python. So when we pass a single string as join() method input, its characters are the iterable elements.

该字符串在Python中是可迭代的。 因此,当我们传递单个字符串作为join()方法输入时,其字符是可迭代的元素。

4.字符串与set()的join() (4. String join() with Set)

vowels_set = set(('a', 'e', 'i', 'o', 'u'))print(" ".join(vowels_set))

Output:

输出:

u i e o a

Python set is an unordered collection, so the iteration order is random. You might get a different output in multiple runs.

Python set是无序集合,因此迭代顺序是随机的。 您可能会在多次运行中获得不同的输出。

5. join()的异常 (5. Exception with join())

If the iterable elements are not string, it raises a TypeError.

如果可迭代元素不是字符串,则引发TypeError。

class Data:passd1 = Data()
d2 = Data()list_data = [d1, d2]print(",".join(list_data))

Output:

输出:

TypeError: sequence item 0: expected str instance, Data found

结论 (Conclusion)

The join() method is useful in creating a string representation from the iterable elements. This method returns a new string and the original string and iterable remains unchanged. We can create CSV string as well as a tab-separated string using this method.

join()方法在根据可迭代元素创建字符串表示形式时很有用。 此方法返回一个新字符串,原始字符串和可迭代保持不变。 我们可以使用此方法创建CSV字符串以及制表符分隔的字符串。

进一步阅读 (Further Reading)

  1. Python String FunctionsPython字符串函数
  2. f-strings in PythonPython中的f字符串
  3. String formatting in PythonPython中的字符串格式

翻译自: https://www.journaldev.com/23571/python-string-join

Python字符串join()方法相关推荐

  1. Python 字符串 join 方法

    Python 字符串有两个 join 方法: join():连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 os.path.join():将多个路径组合后返回 ...

  2. python的for语句写新的字符串_python写for循环python字符串排序方法

    一般情况下,python中对一个字符串排序相当麻烦: 一.python中的字符串类型是不允许直接改变元素的.必须先把要排序的字符串放在容器里,如list. 二.python中的list容器的sort( ...

  3. python string 方法,python字符串的方法与操作大全

    一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为true或者fals ...

  4. 如何看待,入门学习Python必看视频?python字符串(string)方法整理

    如何看待,入门学习Python必看视频?哈佛大学教授推荐,python字符串(string)方法整理 哈佛大学推荐,Python基础入门,Python小白书籍,Python学习路线,Python进阶, ...

  5. python的字符串定界符可以使用_使用Template格式化Python字符串的方法

    对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用Template对象来进行格式化. from string ...

  6. python字符串反转方法_Python程序使用堆栈和反转方法反转字符串

    python字符串反转方法 Given a string and we have to reverse it by using stack and by using reversed method i ...

  7. python 字符串find方法怎么用_Python字符串find()方法

    Python字符串find()方法确定字符串str是出现在字符串中,还是在字符串指定范围的子串中,子字符串是由给给定起始索引beg和结束索引end切片得出. 语法 以下是find()方法的语法 - s ...

  8. “Python字符串index()方法应用案例”文末三道思考题答案

    问题链接:Python字符串index()方法应用案例一则 本文给出上文文末三个思考题的参考答案,当然,这些答案不是唯一的,也不是最高效的,只是演示字符串方法和内置函数的用法,并且在原题代码上做最少的 ...

  9. Python字符串对齐方法(ljust()、rjust()和center())详解

    Python字符串对齐方法(ljust().rjust()和center())详解 Python str 提供了 3 种可用来进行文本对齐的方法,分别是 ljust().rjust() 和 cente ...

最新文章

  1. Service Mesh 和 API Gateway 关系深度探讨
  2. golang自带的rpc 服务端
  3. NHibernate Profiler使用方法
  4. jsp实现仿QQ空间新建多个相册名称,向相册中添加照片
  5. SAP License:如何修改科目为为未清项目管理
  6. 语音识别概念午后大跌 语音识别概念股一览表
  7. linux 共享内存 信号量 同步
  8. 【2015-2016 NEERC - G】Graph【构造 + 拓扑排序】
  9. 【MATLAB】基本绘图函数(涵盖所有基本绘图指令)
  10. 有趣的ASCII-Art
  11. 交通流量预测数据集解读
  12. 2020年,这个算法团队都干了啥?
  13. 202302|读书笔记——国图点滴
  14. 【水位预测】基于matlab径向基神经网络地下水位预测【含Matlab源码 1939期】
  15. java集合系列——java集合概述(一)
  16. spark解决Illegal pattern component: XXX NoSuchFieldError: KRYO_SARG_BUFFER
  17. 基于神经网络的预测控制,神经网络预测系统应用
  18. Floorplan后端概念合集
  19. 计算机图形学——二维卡通人物交互设计
  20. 域名解析、映射以及添加SSL证书

热门文章

  1. inner join ,left join ,right join 以及java时间转换
  2. asp.net页面调用cs中的方法
  3. 程序设计基础(C语言)教学案例-序言
  4. 面向对象及os模块、socket模块
  5. spark报错:invalid token
  6. 论文学习: Journaling of Journal is (almost) Free 未整理
  7. finally中关闭资源
  8. Steps And Uses Of Product Costing
  9. 开放源码的.NET 反编译工具 .NET IL调试工具 学习微软中间语言(MSIL)的绝佳工具 Dotnet IL Editor 推荐...
  10. 为什么需要使用Git客户端?