如何避免大写首字母缩略词被.capitalize() 小写? Python

How to avoid capitalised acronyms and abbreviations from being lowercased by .capitalize()? Python

使用 .capitalize() 将句子的第一个字母大写效果很好。除非句子的第一个单词是像 'IBM' 或 'SIM' 这样的首字母缩略词,它们会小写(第一个字母除外)。例如:

L = ["IBM", "announced", "the", "acquisition."]
L = [L[0].capitalize()] + L[1:]
L = " ".join(L)
print(L)

给出:

"Ibm announced the acquisition."

但我想要这样:

"IBM announced the acquisition."

有没有办法避免这种情况 - 例如通过跳过首字母缩略词 - 同时仍然输出如下大写的句子?

"IBM's CEO announced the acquisition."
"The IBM acquisition was announced."

只需将第一个单词的第一个字符大写:

L = ["IBM", "announced", "the", "acquisition."]
L[0] = L[0][0].upper() + L[0][1:] #Capitalizes first letter of first word
L = " ".join(L)

print(L)
>>>'IBM announced the acquisition.'