撇号后python不要大写 [英] python dont capitalize after apostrophe

查看:99
本文介绍了撇号后python不要大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇文章,所以对我轻松一点.我正在努力让我在上课的时候回到餐厅的名字,就像标题一样.我遇到的问题是乔的问题,当我使用title()时,乔回到了首字母S的状态.当我使用capitalize()时,Joe的情况很好,但汉堡王又以小写的k作为Burger king回来了.我正在尝试找出简化方法,以便每个单词都具有大写字母,而撇号后面没有大写的S.我正在研究的示例来自Python Crash Course第9章.我正在使用python 3.xx版本运行Geany.感谢您的所有帮助.

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

推荐答案

只需拆分并加入open_restaurant

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

这篇关于撇号后python不要大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆