python字符串格式,包含列表和字符串 [英] python string format with both list and string

查看:53
本文介绍了python字符串格式,包含列表和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用字符串格式将变量值插入到 mystring 中,其中一些变量是正常值,一些是列表值.

myname = 'tom'mykids = ['aa', 'bb', 'cc']mystring = """ 你好,我的名字是 %s,这是我的孩子 %s, %s, %s """%(我的名字,元组(mykids))

我得到的错误是没有足够的参数,因为我可能做错了 tuple(mykids).感谢帮助.

解决方案

您可以使用 str.format() 代替:

<预><代码>>>>我的名字 = '汤姆'>>>mykids = ['aa','bb','cc']>>>mystring = '你好,我的名字是 {},这是我的孩子 {}, {}, {}'.format(myname, *mykids)>>>打印我的字符串你好,我叫汤姆,这是我的孩子们 aa, bb, cc

注意使用 *mykids 解包列表并将每个列表项作为单独的参数传递给 format().

但是请注意,格式字符串被硬编码为仅接受 3 个孩子.更通用的方法是使用 str.join() 将列表转换为字符串:

<预><代码>>>>mystring = '你好,我的名字是 {},这是我的孩子 {}'.format(myname, ', '.join(mykids))>>>打印我的字符串你好,我叫汤姆,这是我的孩子们 aa, bb, cc>>>mykids.append('dd')>>>mystring = '你好,我的名字是 {},这是我的孩子 {}'.format(myname, ', '.join(mykids))>>>打印我的字符串你好,我叫汤姆,这是我的孩子们 aa, bb, cc, dd

后一种方法也适用于字符串插值:

<预><代码>>>>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))>>>打印我的字符串你好,我叫汤姆,这是我的孩子 aa

I 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆