使用.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."发布于 2018-06-04 15:17:16
只需大写第一个字的第一个字符:
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.'https://stackoverflow.com/questions/50683716
复制相似问题