问题
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