我正在做一个作业,我不太清楚如何指定我希望项目分隔符处理的字符串。
代码行为:
print('Customer ordered', vCookieOrdered, 'vanilla cookie(s) for the price of $',
format(vCookieOrderedCost, '.2f'), sep='')
我希望它显示:
"Customer ordered x vanilla cookie(s) for the price of $1.20"
但我无法让分隔符只消除$
后的空间.
快速解决方案:
print('Customer ordered {} vanilla cookie(s) for the
price of ${}'.format(vCookieOrdered, vCookieOrderedCost))
解释:
我建议您使用.format
,这使您不必使用sep=" "
,并让您更好地控制如何编辑文本。这是一个包含许多交互式示例的优秀资源。 Note: Do a quick search for .format so that you don't have to waste time scrolling through the page
您需要检查如何使用' .format
' 方法。请参阅一些示例。https://docs.python.org/3.6/library/string.html#format-examples
print('Customer ordered {order:} vanilla cookie(s) for the price of $ {cost:.2f}'.format(order=vCookieOrdered, cost=vCookieOrderedCost))
或
text = 'Customer ordered {order:} vanilla cookie(s) for the price of $ {cost:.2f}'
print(text.format(order=vCookieOrdered, cost=vCookieOrderedCost))