Search code examples
pythonpython-3.xmethodsstring-concatenationf-string

actually i want to use a title method over a text in f-string?


     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)

#So the problem here is that the concatination works nice but i don't know how to apply the title method to the initial text inside the f-string syntax without assigning that text to an extra variable.


Solution

  • I'll admit it's a bit weird with 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 would be more appropriate here I think:

    "and by that much money i want to own a {} & a {}".title().format(my_favourites[1].title(), my_favourites[0].upper())