這裏是我的代碼:善用每個句子的第一個字母在Python
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
沒有被資本化,因爲我劈在.
和這句話有一個以上這段時間後的空間,但我不知道我該如何解決這個問題,或者如果有更好的方式去做我正在做的事情。任何幫助將不勝感激。
你想之前,「也許」保留4空間或者更改標準1個空間? – Skycc
@Skycc我想保留4個空格。剛剛通過某人的幫助編輯了我的問題,所以更清楚。 – Kblo55
http://stackoverflow.com/questions/26320697/capitalization-of-each-sentence-in-a-string-in-python-3 –