python dont capitalize after apostrophe [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-01-04 01:36:48

问题


first post so go easy on me. I am trying to make it to where when I run my class the name of the restaurant comes back like a title. The problem I ran into was with joe's it comes back as Joe'S with a capital S when I use title(). When I use capitalize() Joe's comes back fine but burger king comes back as Burger king with a lower case k. I am trying to find out how to simplify this so I can have the capitalized letter of each word, without capitalizing the S after the apostrophe. The example I am working on is from Python Crash Course chapter 9. I am running Geany with python version 3.xx. Thanks for all the help.

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()

回答1:


Just split and join in 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()


来源:https://stackoverflow.com/questions/41484311/python-dont-capitalize-after-apostrophe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!