Skip to content

Template's t syntax is abnormal. #139824

@yunhai-dev

Description

@yunhai-dev

Bug report

Bug description:

a = 'tom'
tp = t'hello world! {a}'
tp2 = t'hello world! {'tom'}'
print(tp)
print(tp2)

In the current form, the variable a inside the braces and the literal 'tom' are both strings with the same content, but in actual use a problem occurs. The printed result is as follows:

Template(strings=('hello world! ', ''), interpolations=(Interpolation('tom', 'a', None, ''),))
Template(strings=('hello world! ', ''), interpolations=(Interpolation('tom', "'tom'", None, ''),))

I think this is a highly counterintuitive (very user-unfriendly) issue: it treats the quotation marks as part of the content and includes them in the expression.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions