python字符串格式,包含列表和字符串 [英] python string format with both list and string
问题描述
我想使用字符串格式将变量值插入到 mystring 中,其中一些变量是正常值,一些是列表值.
myname = 'tom'mykids = ['aa', 'bb', 'cc']mystring = """ 你好,我的名字是 %s,这是我的孩子 %s, %s, %s """%(我的名字,元组(mykids))
我得到的错误是没有足够的参数,因为我可能做错了 tuple(mykids)
.感谢帮助.
您可以使用 str.format()
代替:
注意使用 *mykids
解包列表并将每个列表项作为单独的参数传递给 format()
.
但是请注意,格式字符串被硬编码为仅接受 3 个孩子.更通用的方法是使用 str.join()
将列表转换为字符串:
后一种方法也适用于字符串插值:
<预><代码>>>>mystring = '你好,我的名字是 %s,这是我的孩子 %s' % (myname, ', '.join(mykids))>>>打印我的字符串你好,我叫汤姆,这是我的孩子们 aa, bb, cc, dd最后你可能想要处理只有一个孩子的情况:
<预><代码>>>>one_kid = '这是我的孩子'>>>many_kids = '这些是我的孩子'>>>mystring = 'hello my name is {} and {} {}'.format(myname, many_kids if len(mykids) > 1 else one_kid, ', '.join(mykids))>>>打印我的字符串你好,我叫汤姆,这些是我的孩子 aa、bb、cc、dd>>>mykids = ['aa']>>>mystring = 'hello my name is {} and {} {}'.format(myname, many_kids if len(mykids) > 1 else one_kid, ', '.join(mykids))>>>打印我的字符串你好,我叫汤姆,这是我的孩子 aaI want to use string formatting to insert variable values into mystring where some of the variables are normal values and some are list values.
myname = 'tom'
mykids = ['aa', 'bb', 'cc']
mystring = """ hello my name is %s and this are my kids %s, %s, %s """
% (myname, tuple(mykids))
I get the error not enough arguments because i probably did the tuple(mykids)
wrong. help appreciated.
You can use str.format()
instead:
>>> myname = 'tom'
>>> mykids = ['aa','bb','cc']
>>> mystring = 'hello my name is {} and this are my kids {}, {}, {}'.format(myname, *mykids)
>>> print mystring
hello my name is tom and this are my kids aa, bb, cc
Note the use of *mykids
which unpacks the list and passes each list item as a separate argument to format()
.
Notice, however, that the format string is hardcoded to accept only 3 kids. A more generic way is to convert the list to a string with str.join()
:
>>> mystring = 'hello my name is {} and this are my kids {}'.format(myname, ', '.join(mykids))
>>> print mystring
hello my name is tom and this are my kids aa, bb, cc
>>> mykids.append('dd')
>>> mystring = 'hello my name is {} and this are my kids {}'.format(myname, ', '.join(mykids))
>>> print mystring
hello my name is tom and this are my kids aa, bb, cc, dd
The latter method also works with string interpolation:
>>> mystring = 'hello my name is %s and this are my kids %s' % (myname, ', '.join(mykids))
>>> print mystring
hello my name is tom and this are my kids aa, bb, cc, dd
Finally you might want to handle the case where there is only one child:
>>> one_kid = 'this is my kid'
>>> many_kids = 'these are my kids'
>>> mystring = 'hello my name is {} and {} {}'.format(myname, many_kids if len(mykids) > 1 else one_kid, ', '.join(mykids))
>>> print mystring
hello my name is tom and these are my kids aa, bb, cc, dd
>>> mykids = ['aa']
>>> mystring = 'hello my name is {} and {} {}'.format(myname, many_kids if len(mykids) > 1 else one_kid, ', '.join(mykids))
>>> print mystring
hello my name is tom and this is my kid aa
这篇关于python字符串格式,包含列表和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!