This question already has answers here:
Python title() with apostrophes
(5个答案)
两年前关闭。
先发个帖子,别对我太客气。我试着在我上课的时候把餐厅的名字写出来。我遇到的问题是,当我使用title()时,joe's的it返回为带有大写s的joe's。当我使用大写()时,Joe's会很好地返回,但burger king会返回小写字母k的burger king。我正试图找出如何简化此操作,以便可以得到每个单词的大写字母,而不必在撇号后大写s。我正在研究的示例来自Python崩溃课程的第9章。我用python 3.xx版本运行Geany。谢谢你的帮助。
class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        """Initialize name and cuisine type"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(self.restaurant_name.title() + " serves " + self.cuisine_type)

    def open_restaurant(self):
        print(self.restaurant_name.capitalize() + " is now open!")

restaurant = Restaurant('joe\'s', 'mexican')
burger_king = Restaurant('burger king', 'burgers')
restaurant.describe_restaurant()
restaurant.open_restaurant()
burger_king.describe_restaurant()
burger_king.open_restaurant()

最佳答案

就分头加入开放式餐厅吧

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        """Initialize name and cuisine type"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(self.restaurant_name.title() + " serves " + self.cuisine_type)

    def open_restaurant(self):
        Name = self.restaurant_name
        print(' '.join([x.capitalize() for x in Name.split(' ')]) + " is now open!")

restaurant = Restaurant('joe\'s', 'mexican')
burger_king = Restaurant('burger king', 'burgers')
restaurant.describe_restaurant()
restaurant.open_restaurant()
burger_king.describe_restaurant()
burger_king.open_restaurant()

10-04 22:53
查看更多