Before Python 3.12:
For f"{var.replace("-","")}text123"
, Python parses f"{var.replace("
as a complete string, which you can see has an opening {
and opening (
, but then the string is terminated. It first expected a )
and eventually a }
, hence the error you see.
To fix it, Python allows '
or "
to enclose a string, so use one for the f-string and the other for inside the string:
f"{var.replace('-','')}text123"
or:
f'{var.replace("-","")}text123'
Triple quotes can also be used if internally you have both '
and "
f'''{var.replace("-",'')}text123'''
or:
f"""{var.replace("-",'')}text123"""
As of Python 3.12:
Some f-string limitations have been lifted and the OP's original code now works:
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> var="ab-c"
>>> f"{var.replace("-","")}text123"
'abctext123'
See What's New in Python 3.12, PEP 701: Syntactic formalization of f-strings.