如何将可变长度列表插入字符串
我认为我认为是Python中的一个基本问题:
我有一个列表,该列表的长度可以变化,我需要将其插入字符串中以备后用。 格式很简单,我只需要在名称围绕名称的每个名称和括号的每个名称之间进行逗号。
List = ['name1', 'name2' .... 'nameN']
string = "Their Names are <(name1 ... nameN)> and they like candy.
示例:
List = ['tom', 'jerry', 'katie']
print(string)
Their Names are (tom, jerry, katie) and they like candy.
对此有什么想法吗?感谢您的帮助!
I have what I think is a basic question in Python:
I have a list that can be variable in length and I need to insert it into a string for later use.
Formatting is simple, I just need a comma between each name up to nameN and parenthesis surrounding the names.
List = ['name1', 'name2' .... 'nameN']
string = "Their Names are <(name1 ... nameN)> and they like candy.
Example:
List = ['tom', 'jerry', 'katie']
print(string)
Their Names are (tom, jerry, katie) and they like candy.
Any ideas on this? Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以实现这一目标。
您可以使用print +格式 +与@forcebru的示例相似。
使用格式可以使其与Python2和Python3兼容。
There are numerous ways to achieve that.
You could use print + format + join similar to the example from @ForceBru.
Using format would make it compatible with both Python2 and Python3.