当前位置:网站首页>String concatenation
String concatenation
2022-08-05 20:23:00 【God's children are singing】
Foreword:
Author: The Children of God Are Singing
Hi, my name is Zhi
Introduction
In python, strings can be concatenated by + sign, join method , and % sign
But in python, use plus sign + to connect strings as little as possible, because String objects are fixed-length objects. Once created, the length cannot be changed. If you use + sign to connect two strings, it will open up a new section of memory with the total length of the original length, and then memcpy the two strings into it.If you want to connect N String objects, you need to perform N-1 memory allocations and copies.The official recommendation is to use the join method of strings. This method is very effective for connecting elements in a list or tuple. It will first count the lengths of all elements, apply for memory, and then copy.
If you want to splicing characters in a loop, I use string formatting % operator display
Use
sd = "God" + "child" + "are singing"print(sd)sb_list = ["God", "Kids", "are singing"]print(''.join(sb_list))sb_replace = '%s%s%s' %("God", "children", "all singing")print(sb_replace)sb_replace = "{}{}{}".format("God", "child", "bothSinging")print(sb_replace)
References
Three common and most efficient Python string concatenation methods
Several ways to concatenate strings in Python
python lists combined into strings
My csdn blog: https://blog.csdn.net/weixin_46654114
Reprint instructions: Explain to me, be sure to indicate the source, and attach a link to my blog.
边栏推荐
猜你喜欢

新手程序员,买一台个人服务器究竟都能做哪些事呢?

Detailed explanation of RAID disk array

【StoneDB子查询优化】subquery子查询-exists子查询的剔除遍历处理

极光推送之自定义声音踩坑记录(持续更新)

华为ADS获取转化跟踪参数报错:getInstallReferrer IOException: getInstallReferrer not found installreferrer

SteamCMD是什么怎么用开服教程

arm设备上的嵌入式开发编译环境搭建
![vi learning (1) [introduction/basic usage/work mode]](/img/9d/059c7679e60b2509ff5536040201aa.png)
vi learning (1) [introduction/basic usage/work mode]

住宅代理随用随付——小型项目的最佳选择

图床软件要收费,算了我自己写一个开源免费的。
随机推荐
SwiftUI case: size adaptive text box
vi learning (1) [introduction/basic usage/work mode]
Codeforces: d. Chip Move "dp + reverse thinking to consider"
第十五天(下)安装newman与jenkins使用
什么是好的应用程序?
【idea插件】MetricsReloaded使用
AI 创作,没有灵魂?
SwiftUI案例:天气
【StoneDB子查询优化】subquery子查询-内存拷贝分析及优化
vi学习(1)【简介/基本使用/工作模式】
Pytest Learning - The Best Way to Use Fixture
【StoneDB子查询优化】subquery子查询-exists子查询的剔除遍历处理
PID 控制理论
【图像分类】Vision Transformer理论解读+实践测试
Taishan OFFICE Technical Lecture: According to the practice of WORD, the schematic diagram of the effect of shading and highlighting borders
【开发者必看】【push kit】推送服务典型问题合集2
2022年最新最全面的Android面试题含答案「建议收藏」
Shell(5)数组
图床软件要收费,算了我自己写一个开源免费的。
国产手机羡慕,中国年轻人追捧iPhone,推动苹果成最赚钱科技企业