共计 912 个字符,预计需要花费 3 分钟才能阅读完成。
在 Python 中,连接是将字符串首尾相连的操作。例如,字符串“Encoding”和字符串“Compiler”可以连接以形成字符串“Encoding Compiler”。该操作是通过使用“+”运算符来完成的。
连接是编程中一个非常重要的概念,因为它允许从较小的片段创建字符串。
通过使用“+”运算符
“+”运算符用于连接两个字符串。此操作可以在名为“ConcatenateStrings.py”的程序中演示。
例如:
s1="Encoding"
s2="Compiler"
s3=s1+s2
s4=s1+" "+s2
print(s3)
print(s4)
我们得到上面例子的输出是:
EncodingCompiler
Encoding Compiler
我们还可以使用不同的方法来执行相同的操作,如下所示:
通过使用 % 运算符
我们可以使用“%”运算符合并两个字符串。让我们看下面的例子:
例如
s1="Encoding"
s2="Compiler"
s3="%s %s"%(s1,s2)
s4="%s%s"%(s1,s2)
print(s3)
print(s4)
我们得到上面例子的输出是:
EncodingCompiler
Encoding Compiler
相关问题:
TypeError:string index out of range python
Error: Conda command not found
TypeError: only length-1 arrays can be converted to Python scalars 文章来源:https://www.toymoban.com/diary/python/444.html
文章来源地址 https://www.toymoban.com/diary/python/444.html
到此这篇关于如何在 Python 中连接两个字符串?的文章就介绍到这了, 更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持 TOY 模板网!
原文地址:https://www.toymoban.com/diary/python/444.html
如若转载,请注明出处:如若内容造成侵权 / 违法违规 / 事实不符,请联系站长进行投诉反馈,一经查实,立即删除!