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

标签 python string python-3.x capitalization

使用 .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/

相关文章:

python - 将字符串列表 [ '3' , '1' , '2' ] 转换为排序整数列表 [1, 2, 3]

python - 如何在 os.walk() 中排除目录?

python - 使用 python3 在文本文件中查找信息

python - future 警告 : Arrays of bytes/strings is being converted to decimal numbers if dtype ='numeric'

c# - 将工作日缩写字符串(包括范围)转换为 List<DayOfWeek>

c++ - _T( ) 宏更改为 UNICODE 字符数据

python - 使用 Open CV 屏蔽水平线和垂直线

python-3.x - Flask-restplus:如何使用 'allOf' 操作定义嵌套模型?

Python:返回元组或列表?

Python 请求花费的时间比预期长得多