Python f-strings: Faster than Plus Sign Concatenation?

preview_player
Показать описание
What is better to concatenate strings: plus sign or f-strings? What about performance?

-----

Рекомендации по теме
Комментарии
Автор

I hadn't expected them to be more performant. But I think a stronger reason to like f-strings is that they do the string coercion for you. It's less prone to mistakes:

b = 1 # integer
a = "some text" + b # TypeError: can only concatenate str (not "int") to str
a = "some text" + str(b) # this is fine, but annoying to remember to str() everything
a = f"some text {b}" # this is fine, and less annoying to write

laubannenberg