join() is a string method and while using it the separator string iterates over an arbitrary sequence, forming string representations of each of the elements, inserting itself between the elements.

join()是一个字符串方法,使用它时,分隔符字符串会在任意序列上迭代,从而形成每个元素的字符串表示形式,并在元素之间插入自己。

Concisely, it's because join and string.join() are both generic operations that work on any iterable and is a string method.

简而言之,这是因为join和string.join()都是可在任何可迭代对象上使用的通用操作,并且是字符串方法。

Because it is a string method, the join() can work for the Unicode string as well as plain ASCII strings.

因为它是一个字符串方法,所以join()可以用于Unicode字符串以及普通ASCII字符串。

Example Usage of string.join(list)

示例string.join(list)的用法

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test_string = "test"
>>> test_string.join("1234")
'1test2test3test4'
>>>

In the above example, the string "test" is joined with every character provided as a join argument.

在上面的示例中,字符串“ test”与作为连接参数提供的每个字符连接在一起。

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test_string = "test"
>>> test_string.join("---")
'-test-test-'
>>>

翻译自: https://www.includehelp.com/python/why-is-it-string-join-list-instead-of-list-join-string.aspx

为什么在Python中使用string.join(list)而不是list.join(string)?相关推荐

  1. python中使用函数的优点_在python中使用自定义初始化函数而不是`__init__`的好处...

    野外的一些API(例如setuptools内部)有类似的东西,他们使用它们的优势. __init__调用可以用于低级内部API,而公共构造函数被定义为类方法,用于构造对象的不同方式.例如,在 pkg_ ...

  2. 为什么是string.join(list)而不是list.join(string)?

    这一直使我感到困惑. 看起来这样会更好: my_list = ["Hello", "world"] print(my_list.join("-&quo ...

  3. python中的运算符重载_Python中的操作符重载

    Python部落组织翻译,禁止转载,欢迎转发. BY ALOK THAKUR · FEBRUARY 22, 2016 2016年2月22日 , ALOK THAKUR写 Python支持许多内建的类型 ...

  4. python中的字符串是什么,一文秒懂Python中的字符串

    摘要:本文将告诉您Python中的字符串是什么,并向您简要介绍有关该概念的所有知识. 因此,让我们开始吧. 什么是Python中的字符串? 我们许多熟悉C,C ++等编程语言的人都会得到诸如" ...

  5. 知道Python中的字符串是什么吗?

    什么是Python中的字符串? 我们许多熟悉C,C ++等编程语言的人都会得到诸如"字符串是字符的集合或字符数组"的答案. 在Python中也是如此,我们说的是String数据类型 ...

  6. Python中int(input(请输入一个数))报错:ValueError: invalid literal for int() with base 10: '2.7'

    编写下面这段代码: def division():apple = int(input("请输入苹果个数:\n"))children = int(input("请输入孩子个 ...

  7. python中线程里面多线程_Python中的线程和多线程是什么

    一.线程的概念 一个进程里面至少有一个控制线程,进程的概念只是一种抽象的概念,真正在CPU上面调度的是进程里面的线程,就好比真正在地铁这个进程里面工作的实际上是地铁里面的线程,北京地铁里面至少要有一个 ...

  8. [转载] Python中的set(集合)数据类型

    参考链接: Python集合set add函数 Python中的set(集合)数据类型 Python中除了常见的Numbers(数字).String(字符串).List(列表).Tuple(元组).D ...

  9. 在Python中查找字符串长度

    介绍 (Introduction) In this tutorial, we are going to discuss how we can find string length in Python. ...

  10. 在Python中使用Twitter Rest API批量搜索和下载推文

    数据挖掘 , 编程 (Data Mining, Programming) Getting Twitter data 获取Twitter数据 Let's use the Tweepy package i ...

最新文章

  1. slot多作用域 vue_vue 深度长文之slot 篇
  2. java 对象与二进制互转
  3. Mysql允许root用户远程访问
  4. dbtool一bug跟踪记
  5. Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
  6. cocos2d中CCSprite的使用总结 【转】
  7. Jenkins ERROR: Server rejected the 1 private key(s)
  8. 经常被问到的有深度有内涵的数据结构面试题
  9. 关于windows桌面网络图标显示未连接却可以上网的情况
  10. hdr_beg(host) hdr_reg(host) hdr_dom(host)
  11. 基于java web和echarts的数据可视化项目
  12. 【源码解读】BertLayer
  13. CODOSYS之结构化文本(ST)—— 初级篇(三)基础-数据类型
  14. R 语言 4.2.2安装 WGCNA
  15. CodeForces 839A Arya and Bran
  16. 可视化讲解:什么是拉丁方阵问题?
  17. 网络学习笔记之IP地址
  18. CAS:2183440-52-8,Cyanine5.5 azide是染料叠氮化物
  19. ubuntu20.04开机进入emergency mode解决办法
  20. SQL语句——分组函数和分组查询

热门文章

  1. iText报表Java_(例)Java生成PDF报表 iText
  2. g标签 怎么设置svg_SVG g元素
  3. python提取数据库数据_Python如何读取MySQL数据库表数据
  4. .size .shape .size() type的运用
  5. 大型网站HTTPS 实践(一)| HTTPS 协议和原理
  6. generator 1【矩阵快速幂】
  7. 016 pickle
  8. VirtualBox 上安装Debian 后分辨率设置
  9. Python 核心编程(第二版)——条件和循环
  10. 看清美国“黑客帝国”的真面目