Python字符串join()方法

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:
    pass


d1 = 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 Functions

    Python字符串函数
  2. f-strings in Python

    Python中的f字符串
  3. String formatting in Python

    Python中的字符串格式

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

  • 14
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值