This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: f-strings don't change the values as expected.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rafael.capucho, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-02-27 17:25 by rafael.capucho, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
crazy.png rafael.capucho, 2017-02-27 17:25
file.py rafael.capucho, 2017-02-27 17:37
Messages (7)
msg288649 - (view) Author: Rafael Capucho (rafael.capucho) Date: 2017-02-27 17:25
In the attached file, the lines 4 and 6 have the same structure, the line 4 changes the value of {a} properly, the line 6 didn't.

Thank you.
msg288651 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-27 17:28
Please provide your example as a text.
msg288654 - (view) Author: Rafael Capucho (rafael.capucho) Date: 2017-02-27 17:37
```
a = "some_underscored_value"


u = (f" hello `{a}` cruel world"
     " hi")

print(u)

query = (f"SELECT COUNT(*) "
         "FROM `{a}` entry "
         "WHERE entry.type == 'device' "
         "AND entry.instance == {a}")

print(query)
```
msg288655 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-27 17:46
What you get? What you expect?
msg288657 - (view) Author: Rafael Capucho (rafael.capucho) Date: 2017-02-27 17:48
I got:

SELECT COUNT(*) FROM `{a}` entry WHERE entry.type == 'device' AND entry.instance == {a}

I expect the value of `{a}` changed, like:

SELECT COUNT(*) FROM `some_underscored_value` entry WHERE entry.type == 'device' AND entry.instance == some_underscored_value
msg288658 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-27 17:58
The expression is a concatenation of f-string expression f"SELECT COUNT(*) " and three string literals. If you want to substitute the "a" value, convert string literals into f-string expressions:

query = (f"SELECT COUNT(*) "
         f"FROM `{a}` entry "
         f"WHERE entry.type == 'device' "
         f"AND entry.instance == {a}")

or

query = ("SELECT COUNT(*) "
         f"FROM `{a}` entry "
         "WHERE entry.type == 'device' "
         f"AND entry.instance == {a}")

But be aware that using f-string expressions for formatting SQL queries is not safe in general case. Instead, use the DB-API’s parameter substitution.
msg288659 - (view) Author: Rafael Capucho (rafael.capucho) Date: 2017-02-27 18:15
Serhiy Storchaka,

Thank you for your explanation.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73854
2017-02-27 18:15:17rafael.capuchosetmessages: + msg288659
2017-02-27 17:58:47serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg288658

stage: resolved
2017-02-27 17:48:50rafael.capuchosetmessages: + msg288657
2017-02-27 17:46:08serhiy.storchakasetmessages: + msg288655
2017-02-27 17:37:10rafael.capuchosetfiles: + file.py

messages: + msg288654
2017-02-27 17:28:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg288651
2017-02-27 17:25:39rafael.capuchocreate