在我的点之后为什么不把这个词大写?

时间:2017-09-24 02:05:20

标签: python-2.7 class capitalization

class Capitalize:

    def __init__(self, capitalize):
        self.capitalize = capitalize.capitalize()
        #self.capitalize_dot =
        def cap():
            list =  self.capitalize.split('.')
            list[item[1].capitalize]


name = Capitalize('simon.hello')
print(name.capitalize)
>>>>Simon.hello

我希望你好也是大写的。我不知道我的代码有什么问题。

2 个答案:

答案 0 :(得分:0)

read the fine manual

  

str.capitalize()

     

返回字符串的副本,其第一个字符大写,其余小写

答案 1 :(得分:0)

您永远不会设置在函数cap()中创建的值,也永远不会调用它!

我认为最好像在函数cap()中创建一个数组但不使用函数,然后使用此值使用for并将每个单词大写,最后使用join来设置self.capitalize

class Capitalize:
    def __init__(self, capitalize):
        # the array of words capitalized
        wordsCapitalize = []
        # the array of words that you send and convert to array using the '.'
        words = capitalize.split('.') 
        # Iterate over the array of words
        for word in words:
            # add new value to the array of words capitalized
            # word is capitalize with function capitalize()
            wordsCapitalize.append(word.capitalize())
        # set the value to self.capitalize using '.' like character to join the values of array with words capitalized
        self.capitalize = '.'.join(wordsCapitalize)

name = Capitalize('simon.hello')
print(name.capitalize)

告诉我:

Simon.Hello

我为begginers编写了最简单的代码。