python字符串追加字符

Python string object is immutable. So every time we use + operator to concatenate two strings, a new string is created.

Python字符串对象是不可变的。 因此,每次我们使用+运算符连接两个字符串时,都会创建一个新字符串。

If we have to append many strings, using + operator will unnecessarily create many temporary strings before we have the final result.

如果我们必须追加许多字符串,则使用+运算符将不必要地创建许多临时字符串,直到获得最终结果。

Python字符串追加 (Python String Append)

Let’s look at a function to concatenate a string ‘n’ times.

让我们看一下一个将字符串连接n次的函数。

def str_append(s, n):output = ''i = 0while i < n:output += si = i + 1return output
timeit module to test the performance. If you simply want to concatenate a string 'n' times, you can do it easily using timeit模块测试性能。 如果只想将字符串连接'n'次,则可以使用s = 'Hi' * 10.s = 'Hi' * 10轻松实现。

Another way to perform string append operation is by creating a list and appending strings to the list. Then use string join() function to merge them together to get the result string.

执行字符串追加操作的另一种方法是通过创建列表并将字符串追加到列表中。 然后使用字符串join()函数将它们合并在一起以获得结果字符串。

def str_append_list_join(s, n):l1 = []i = 0while i < n:l1.append(s)i += 1return ''.join(l1)

Let's test these methods to make sure they are working as expected.

让我们测试这些方法,以确保它们按预期工作。

if __name__ == "__main__":print('Append using + operator:', str_append('Hi', 10))print('Append using list and join():', str_append_list_join('Hi', 10))# use below for this case, above methods are created so that we can# check performance using timeit moduleprint('Append using * operator:', 'Hi' * 10)

Output:

输出:

Append using + operator: HiHiHiHiHiHiHiHiHiHi
Append using list and join(): HiHiHiHiHiHiHiHiHiHi
Append using * operator: HiHiHiHiHiHiHiHiHiHi

在Python中附加字符串的最佳方法 (Best way to append strings in Python)

I have both the methods defined in string_append.py file. Let's use timeit module to check their performance.

我在string_append.py文件中定义了两种方法。 让我们使用timeit模块检查其性能。

$ python3.7 -m timeit --number 1000 --unit usec 'import string_append' 'string_append.str_append("Hello", 1000)'
1000 loops, best of 5: 174 usec per loop
$ python3.7 -m timeit --number 1000 --unit usec 'import string_append' 'string_append.str_append_list_join("Hello", 1000)'
1000 loops, best of 5: 140 usec per loop$ python3.7 -m timeit --number 1000 --unit usec 'import string_append' 'string_append.str_append("Hi", 1000)'
1000 loops, best of 5: 165 usec per loop
$ python3.7 -m timeit --number 1000 --unit usec 'import string_append' 'string_append.str_append_list_join("Hi", 1000)'
1000 loops, best of 5: 139 usec per loop

摘要 (Summary)

If there are few strings, then you can use any method to append them. From a readability perspective, using + operator seems better for few strings. However, if you have to append a lot of string, then you should use the list and join() function.

如果字符串很少,则可以使用任何方法附加它们。 从可读性的角度来看,对于少数几个字符串,使用+运算符似乎更好。 但是,如果必须附加很多字符串,则应使用list和join()函数。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23689/python-string-append

python字符串追加字符

python字符串追加字符_Python字符串追加相关推荐

  1. Java-11-访问字符串中字符与字符串长度

    Java访问字符串中字符与字符串长度 String.charAt(index) String.length() package 字符串的盛宴;public class AccessesCharacte ...

  2. [转载] python 字符串包含某个字符_python字符串

    参考链接: Python字符串capitalize() str字符串 本节内容概览 1.何为str?2.转义字符3.字符串格式化4.Python字符串内建函数和操作5.python字符串练习 一.字符 ...

  3. python字符串中添加字符_Python字符串中添加、插入特定字符

    分析 我们将添加.插入.删除定义为: 添加: 在字符串的后面或者前面添加字符或者字符串 插入: 在字符串之间插入特定字符 在Python中,字符串是不可变的.所以无法直接删除.插入字符串之间的特定字符 ...

  4. python中替换字符串中字符_python替换字符串中的某个字符

    python_split_strip_replace使用方法 使用python时会经常要对字符串做一些处理,比如:分割字符串.去掉空格.替换字符串 中的某个字符等,下面介绍下这几个功能的使用. 一.  ...

  5. python变量和字符_Python变量和字符串

    我需要知道Python中的变量是字符串(名称)还是数字.我想检查图的度数,但是我需要知道"I"迭代器是字符串内部的一个数字还是字符串内的一个名称,在末尾显示了图的度数.在 这个代码 ...

  6. python设置字符_python字符串操作

    # FirstPython.py """ 标准字符串函数 """ """ 字符串索引 "" ...

  7. python定界符有哪些_Python字符串

    第二讲 list 列表 一.列表的基本介绍 什么是列表? 列表由一系列按特定顺序排列的元素组成,使用[]作为定界符,用逗号作为元素分隔符. List的特点 有序可变,可以是任意类型数据. 列表的数据结 ...

  8. python 字符串赋值操作_python字符串操作

    字符串 简介 字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能改变 这里的的改变,指的是声明变量后的真实对象.但如果第二次用到变量,赋值,系统会默认为你新生成一个变量.比如 ...

  9. python字符串拼接数字_python字符串和数值操作函数大全(非常全)

    字符串和数值型数字的操作大全 1.反斜杠\的使用规则:一般使用表示续行的操作,可以其他符号相结合组成其他的一些使用符号,转义字符\'的使用会识别引号,使得字符串中的引号和外面本来的啊引号相区分. (1 ...

最新文章

  1. 【青少年编程】黄羽恒:我要背单词
  2. 字节跳动小程序技术摘要
  3. 10、Java Swing JComboBox:下拉列表组件
  4. 新鲜出炉的家庭贫富评判标准!快看看你家属于小康家庭吗?
  5. 51nod-猴猴的比赛【莫队,线段树】
  6. 高并发下的 HashMap 为什么会死循环
  7. 【文章】七不出,八不归,原来是这个意思
  8. 网络通信之TCP Client通信(基于Arduino)
  9. opencv-api findHomography
  10. 分布式架构的演进过程,docker面试题汇总
  11. css如何设置div中的内容垂直居中?
  12. 根据二次曲面模型法建立区域高程异常拟合模型
  13. 最好用电脑录音软件推荐
  14. 数学建模——BP神经网络模型Python代码
  15. 不到一个月独自一人开发斗地主游戏(h5 + 安卓 + 苹果)
  16. 理财入门:企业分析(简述)
  17. 【如何删除taskmer.exe进程灰鸽子木马】
  18. win10右键反应慢解决方法介绍【解决方法】
  19. 误发邮件怎么办?发错邮件如何撤回?/
  20. Capstone CS5210|CS5210 HDMI to VGA转换器

热门文章

  1. 对SIL9022/9024的配置
  2. nodejs+express开发blog(2)
  3. android Json解析详解(详细代码)
  4. NSString NSURL
  5. EC++学习笔记(四) 设计与声明
  6. SqlServer发布订阅错误收集
  7. Postman界面介绍及实例(转)
  8. PHP curl函数制 exec_ch和get_headers
  9. 关键字nullable,nonnull,null_resettable,_Null_unspecified详解
  10. HDU 4597 Play Game