如何将字符串中的变量变成小写?

问题描述 投票:0回答:1

我已经在这里看到了这个实验室作业,并且我选择了不同的路线来获得相同的结果。然而,我一直对变量的情况感到困惑。输入以大写形式插入,但是当我在字符串中使用该变量时,我会因为它的大写而感到不舒服。有什么简单的方法可以改变它吗?

menu = {'Oil change' : 35, 'Tire rotation' : 19, 'Car wash' : 7}

service = input('Enter desired auto service:\n') #Zylab enters using Oil change uppercase O

print('You entered:', service)

if (service in menu):
    print('Cost of {}: ${}'.format(service, menu.get(service))) #inserting into the string as uppercase but as part of the sentence needs to be lower case.
else:
    print('Error: Requested service is not recognized')

我尝试在第8行使用:

print('Cost of {}: ${}'.format(service.lower, menu.get(service))) #No good

print('Cost of {}: ${}'.format.lower(service, menu.get(service))) #Not god either

我仍在努力格式化展示位置,或者是否可能。

python-3.x string formatting lowercase
1个回答
0
投票

您可以将第 8 行更改为:

print('Cost of {}: ${}'.format(service.lower(), menu.get(service)))
© www.soinside.com 2019 - 2024. All rights reserved.