python - 将python中的名称大写
问题描述
有没有办法输入一个句子 str 并将其中的所有名称大写?
test_str = 'bitcoin is not dead and ethereum is cool'
我希望将其转换为
test_str = 'Bitcoin is not dead and Ethereum is cool'
这可能吗?我的第一个想法是使用 re 模块来定位名称然后修改它,但后来意识到名称似乎没有特定的模式。
解决方案
如果您有要大写的单词列表,可以按如下方式进行:
names = ['bitcoin', 'ethereum']
test_str = 'bitcoin is not dead and ethereum is cool'
output_str = ' '.join([word.capitalize() if word in names else word for word in test_str.split()])
>>> 'Bitcoin is not dead and Ethereum is cool'
推荐阅读
- simpledateformat - java使用simpledateformat解析一个输入String得到ParseException,而仅仅因为一个空格
- .net-core - 将 FluentValidation 与 ASP.NET Core 3.1 和视图组件集成
- testing - 在 Test Cafe 上设置 Firefox 的推荐人
- docker - 在 VS 代码中调试 docker 映像:找不到 cs 文件:
- django - 如何在MongoDB中使用Django Relational DB的ForeignKey、ManyToManyFields等字段
- scala - 使用 apache spark 和 scala 将数据推送到 Nifi Flow
- javascript - 使用 Chart.js 时隐藏 xAxis 标签
- python - 如何在python数据透视表数据框中的条形图中添加数据标签值
- python - 为什么隐藏对 dash_html_components.Label 不起作用?
- pandas - Groupby 两列的总和并在熊猫中创建新的数据框