![](/img/trans.png)
[英]How can I split concatenated strings that contain no delimiters in python?
[英]How can strings be concatenated?
如何在python中连接字符串?
例如:
Section = 'C_type'
将其与Sec_
连接以形成字符串:
Sec_C_type
你也可以这样做:
section = "C_type"
new_section = "Sec_%s" % section
这样您不仅可以追加,还可以在字符串中的任何位置插入:
section = "C_type"
new_section = "Sec_%s_blah" % section
只是一个评论,因为有人可能会觉得它很有用 - 你可以一次连接多个字符串:
>>> a='rabbit'
>>> b='fox'
>>> print '%s and %s' %(a,b)
rabbit and fox
连接字符串的更有效方法是:
加入():
非常高效,但有点难以阅读。
>>> Section = 'C_type'
>>> new_str = ''.join(['Sec_', Section]) # inserting a list of strings
>>> print new_str
>>> 'Sec_C_type'
字符串格式:
易于阅读,在大多数情况下比“+”连接更快
>>> Section = 'C_type'
>>> print 'Sec_%s' % Section
>>> 'Sec_C_type'
使用+
进行字符串连接:
section = 'C_type'
new_section = 'Sec_' + section
要在python中连接字符串,请使用“+”符号
对于附加到现有字符串结尾的情况:
string = "Sec_"
string += "C_type"
print(string)
结果是
Sec_C_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.