Python 如何连接多个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42149579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I join multiple strings?
提问by Christian Gutierrez
How do I join all the strings in stringList into one without printing it?For example s = joinStrings(['very', 'hot', 'day']) # returns string print(s)Veryhotday'''here is the actual problem my professor gave me
如何将 stringList 中的所有字符串合并为一个而不打印它? 例如 s = joinStrings(['very', 'hot', 'day']) # 返回字符串 print(s)Veryhotday'''这里是实际的教授给我的问题
回答by Nullman
it feels a little backwards, but you join with a chosen uhh seperator
''.join(['your','list','here'])
you can fill in the ''
and it will use what ever is inside between each pair of items i.e '---'.join(['your','list','here'])
will produce your---list---here
感觉有点倒退,但是您加入了一个选定的 uhh 分隔符,
''.join(['your','list','here'])
您可以填写''
它,它将使用每对项目之间的内容,即'---'.join(['your','list','here'])
会产生your---list---here
回答by Jacek Zygiel
You can solve it using single line for loop.
您可以使用单行 for 循环解决它。
def joinStrings(stringList):
return ''.join(string for string in stringList)
Everything is described in Python Documentation: Python Docs
一切都在 Python 文档中进行了描述: Python Docs
E.g.: String join method: Python string methods
例如:字符串连接方法: Python 字符串方法
回答by Amit Malik
All the above solution are good... You can also use extend(object) function...
以上所有解决方案都很好......你也可以使用extend(object)函数......
String1.extend(["very", "hot", "day"] )
Enjoy....
享受....
回答by Jonathan Griffin
Unfortunately, I am only learning python 2.7 so this probably won't help:
不幸的是,我只学习 python 2.7 所以这可能无济于事:
def joinStrings(stringList):
list=""
for e in stringList:
list = list + e
return list
s = ['very', 'hot', 'day']
print joinStrings(s)