使用 .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.'
关于python - 如何避免大写首字母缩略词被.capitalize() 小写? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50683716/