Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用.format()时,是否也可以有条件地使用.capitalize()?_Python_Python 2.7_Renpy - Fatal编程技术网

在Python中使用.format()时,是否也可以有条件地使用.capitalize()?

在Python中使用.format()时,是否也可以有条件地使用.capitalize()?,python,python-2.7,renpy,Python,Python 2.7,Renpy,好吧,这可能有点神秘 我有一份食物清单,我从中随机挑选食物。我在另一个列表中使用它来谈论所选的食物,但是这些字符串可以在句子的开头和结尾都有食物名称 例如: default breakfast_food_list = ['pancakes','bacon and eggs','scones','sandwiches','beans and bacon','quiche','cereal','muffins'] default breakfast_nice_list = [

好吧,这可能有点神秘

我有一份食物清单,我从中随机挑选食物。我在另一个列表中使用它来谈论所选的食物,但是这些字符串可以在句子的开头和结尾都有食物名称

例如:

default breakfast_food_list = ['pancakes','bacon and eggs','scones','sandwiches','beans and bacon','quiche','cereal','muffins']

default breakfast_nice_list = [
            ['I love your {0}',2,'fm_rel'],
            ['Ah, I just love those {0}',2,'fm_rel'],
            ['{0} is fine',1,'fm_rel'],
]

$ breakfast_select = random.randint(0,len(breakfast_food_list)-1)
$ breakfast_nice = breakfast_nice_list[breakfast_nice_select][0]
我会这样展示:

$ breakfast_reply = breakfast_nice.format(breakfast_food)
from random import choice

default breakfast_food_list = ['pancakes','bacon and 
     eggs','scones','sandwiches','beans and bacon','quiche','cereal','muffins']

default breakfast_nice_list = [
    ['I love your {0}',2,'fm_rel'],
    ['Ah, I just love those {0}',2,'fm_rel'],
    ['{0} is fine',1,'fm_rel']]

words = choice(breakfast_nice_list)[0]
food = choice(breakfast_food_list)
output = words.format(food).capitalize()
现在,我可以在结尾处打上.capitalize(),但这会使它大写,即使它在句子的结尾或中间


因此,我正在寻找一种解决方案,当它是句子中的第一个单词,并且只有第一个单词时,它会大写。

编辑:我已根据问题评论(以及此答案)中的输入更新了答案//编辑

您可以尝试以下方法:

$ breakfast_reply = breakfast_nice.format(breakfast_food)
from random import choice

default breakfast_food_list = ['pancakes','bacon and 
     eggs','scones','sandwiches','beans and bacon','quiche','cereal','muffins']

default breakfast_nice_list = [
    ['I love your {0}',2,'fm_rel'],
    ['Ah, I just love those {0}',2,'fm_rel'],
    ['{0} is fine',1,'fm_rel']]

words = choice(breakfast_nice_list)[0]
food = choice(breakfast_food_list)
output = words.format(food).capitalize()

如果您使用的是Python3.6,那么您可能可以重新安排并使用它。像

早餐选择=早餐食物列表[早餐选择]


breaken\u reply=f“{breaken\u choice.capitalize()}很好”

为什么不直接添加行
breaken\u reply[0]。upper()
我是否可以建议使用
from random import choice
从列表中获取一个随机元素来简化?我的意思是
早餐回复=早餐回复[0]。upper()
,还有好的建议Thomas Fauskanger。或者干脆
早餐回复=早餐美味。格式(早餐食物)。大写()
,我不确定我是否遗漏了一个要求。是的,对不起,我的电话搞错了,意思是
。大写()
。谢谢分享,我没有意识到这一点,我主要是在3.6上。然而,这个问题被Python2.7标记为:虽然效率稍低,但可读性可能更高,只需在格式之后调用
.capitalize()
,例如
output=words.format(food).capitalize()
,就可以找到正确的答案。。。我责怪我完全的疲倦使这条路变得比实际情况更复杂。当然,在后面添加
.capitalize()
正是我们所需要的。