博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python字符串join()方法
阅读量:2528 次
发布时间:2019-05-11

本文共 2787 字,大约阅读时间需要 9 分钟。

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)

翻译自:

转载地址:http://bamzd.baihongyu.com/

你可能感兴趣的文章
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>