Menu

Python t-strings (PEP 750): A Practical Tutorial With Real Examples
πŸ“°
0

Python t-strings (PEP 750): A Practical Tutorial With Real Examples

DEV CommunityΒ·Maksim DanilchenkoΒ·about 1 month ago
#DjOK8v9G
#how#python#template#strings#string#fullscreen
Reading 0:00
15s threshold

TL;DR Python 3.14 ships t-strings (PEP 750) , a new string literal that looks like an f-string but returns a Template object instead of a finished str . You get the static parts and the interpolated values separately, so a library author can sanitize, escape, parameterize, or defer the rendering. I rewrote a small SQLite logger I keep on my laptop using t-strings and the diff was about ten lines, but the SQL injection class of bug is now structurally impossible. Library authors will get the most use out of them; application code will mostly read t-strings rather than write them. Why f-strings stop being enough I have been writing Python since 2.6 and f-strings, introduced in 3.6, were a clear win. They replaced % formatting and .format() for almost everything I do. The catch is that f-strings evaluate immediately : the moment you write f"... {x} ..." , Python calls str.__format__ on each interpolated value and concatenates the result.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More