python - 实际上我想对 f 字符串中的文本使用标题方法?
问题描述
my_favourites=['lamborghini',"sea facing resort",
'100 b dollar','3D animator']
line_1='and by that much money i want to own a '.title()+my_favourites[1].title()+' & a
'+my_favourites[0].upper()
print(line_1)
line_2=f'and by that much money i want to own a {my_favourites[1].title()} & a
{my_favourites[0].upper()}'
print(line-2)
#所以这里的问题是连接效果很好,但我不知道如何将标题方法应用于 f-string 语法中的初始文本,而不将该文本分配给额外的变量。
解决方案
我承认 f-strings 有点奇怪:
f"{'and by that much money i want to own a'.title()} {my_favourites[1].title()} & {'a'.title()} {my_favourites[0].upper()}"
str.format
我认为在这里会更合适:
"and by that much money i want to own a {} & a {}".title().format(my_favourites[1].title(), my_favourites[0].upper())
推荐阅读
- c# - 当我开始拖动时,我的可拖动对象变得不可见
- python - How to convert sequence of ply files to .fbx animation
- c++ - Can I learn the Win32 API without C++ (with only C)?
- php - My database connection doesn't connect with postgreSQL
- python - How do i add path in python docx?
- python - 当我的脚本导入模块 pikepdf 和 pdfminer3 时,如何修复 pyinstaller 'no module named...' 错误?
- html - Flex item's auto margin cuts parent's padding
- javascript - 当 x 值与标签重合时,Chartjs 折线图会全部打乱
- java - Do I need to clean up Thread objects in Java?
- ionic-framework - ionic serve 无法正常工作 我该如何解决这个错误?