这是我的代码:
传入的字符串是:
def fix_capitalization(usrStr):
newString = ''
wordList = []
numLetters = 0
for s in usrStr.split('. '):
if s[0].isupper():
s = s.capitalize()
s = s.replace(' i ', " I ")
wordList.append(s)
if s.islower():
s = s.capitalize()
s = s.replace(' i ', " I ")
wordList.append(s)
numLetters += 1
if s[0].islower():
s = s.capitalize()
s = s.replace(' i ', " I ")
wordList.append(s)
numLetters += 1
newString = '. '.join(wordList)
return newString, numLetters
传入的字符串是:
i want some water. he has some. maybe he can give me some. i think I will ask.
请注意,maybe
前面有4个空格。我想要的结果是:
I want some water. He has some. Maybe he can give me some. I think I will ask.
但我得到的是:
I want some water. He has some. maybe he can give me some. I think I will ask.
我知道 maybe
没有被大写,因为我在 .
上进行了拆分,而该句子在句号后有多个空格,但我不确定如何解决这个问题或是否有更好的方法来处理我正在做的事情。任何帮助将不胜感激。