Python template strings

파이썬 템플릿 문자열

Python 3.14 t-strings preserve literal pieces and interpolated values separately.

···
html
<div class="visual template" data-step="0"><div class="visual-head"><strong>PYTHON T-STRING</strong><i></i></div><div class="visual-scene"><div class="tpl-parts"><span>literal</span><span>{input}</span><span>literal</span></div><div class="tpl-process">escape(value)</div><div class="tpl-result mono">&lt;safe output&gt;</div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b><b></b></div></div>
css
.visual{width:min(95vw,760px);height:min(91vh,330px);padding:clamp(9px,2.5vmin,18px);display:flex;flex-direction:column;gap:clamp(6px,1.7vmin,11px);border:1px solid var(--line);border-radius:13px;background:var(--surface);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,-apple-system),sans-serif}
.visual-head{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:19px;color:var(--accent)}.visual-head strong{font-size:1em}.visual-head i{display:block;width:8px;height:8px;flex:none;border-radius:50%;background:var(--accent);animation:visual-pulse 1.3s infinite}@keyframes visual-pulse{50%{opacity:.25}}
.visual-scene{position:relative;flex:1;min-height:0;overflow:hidden}.visual-caption{min-height:2.4em;display:grid;place-items:center;text-align:center}.visual-dots{display:flex;justify-content:center;gap:4px}.visual-dots b{display:block;width:15px;height:3px;border-radius:3px;background:var(--line)}.visual-dots b.on{background:var(--accent)}
.visual-scene *{box-sizing:border-box}.visual-scene .mono{font-family:ui-monospace,monospace}.visual-scene .muted{color:var(--muted)}.visual-scene .lit{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 14%,var(--surface))!important;color:var(--fg)!important}
.template .visual-scene{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:10px}.tpl-parts{display:flex;gap:4px;width:100%;justify-content:center}.tpl-parts span{border:1px solid var(--line);border-radius:6px;padding:7px 5px;background:var(--bg);font:500 12px ui-monospace,monospace;transition:transform .3s,background .3s}.tpl-parts span:nth-child(2){color:var(--accent)}.tpl-process{border-bottom:2px solid var(--accent);padding:2px 12px;font:500 12px ui-monospace,monospace;opacity:.5;transition:opacity .3s}.tpl-result{opacity:.3;transition:opacity .3s}.template[data-step="0"] .tpl-parts span:first-child,.template[data-step="1"] .tpl-parts span:nth-child(2){background:color-mix(in srgb,var(--accent) 14%,var(--surface));transform:translateY(-4px)}.template[data-step="2"] .tpl-process,.template[data-step="3"] .tpl-result{opacity:1;color:var(--accent)}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Literal piece is retained","Input stays separate","Consumer escapes the value","Output is assembled"];let step=0;function advance(){root.dataset.step=String(step);cap.textContent=captions[step];dots.forEach((d,i)=>d.classList.toggle('on',i===step));if(root.classList.contains('generic')){root.querySelector('.generic-input').textContent=step===3?'"hi"':'7';root.querySelector('.generic-output').textContent=step===3?'"hi" : string':'7 : number'}if(root.classList.contains('union'))root.querySelector('.set-value').textContent=step%2?'value: "seven"':'value: 7';step=(step+1)%captions.length}advance();let timer=setInterval(advance,1100);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

A t-string resembles an f-string, but it produces a Template object instead of immediately forming a plain string. Literal pieces and interpolated values stay separate for the consumer to process.

The demo moves an input value through separation and escaping. A template is not automatically safe for HTML or SQL; its consumer must apply the right escaping or parameterization.

When to use

Use it when a template API must inspect or transform interpolated values before rendering.

Open as page ↗