This question already has answers here:
String manipulation: capitalize first letter of every sentence
                                
                                    (14个回答)
                                
                        
                                在8个月前关闭。
            
                    
我一直在尝试为菜单项目创建一个小函数,该函数将字符串作为输入并大写每个句子的首字母。我认为我已经很接近正确了,但是它一直给我一个我似乎无法修复的串联错误。有人知道我哪里出问题了吗?

def fix_capitalization(usrStr):
    s1 = usrStr.split(". ")
    s2 = [s1[0].capitalize() + s1[1:] for i in s1]
    st2 = '. '.join(s2)

    return st2

最佳答案

在列表理解中,您拥有s1 [0]而不是i [0]

def fix_capitalization(usrStr):
    s1 = usrStr.split(". ")
    s2 = [i[0].capitalize() + i[1:] for i in s1]
    st2 = '. '.join(s2)

    return st2

07-26 05:52