f-string: unmatched '(' in line with function call
Question:
I’m trying to use f-strings in python to substitute some variables into a string that I’m printing, and I’m getting a syntax error.
Here’s my code:
print(f"{index+1}. {value[-1].replace("[Gmail]/", '')}")
I only started having the problem after I added the replace. I’ve checked plenty of times and I’m certain that I’m not missing a parenthesis. I know that there are plenty of other ways to accomplish this, some of which are probably better, but I’m curious why this doesn’t work.
Answers:
Seems like this does not work
x = 'hellothere'
print(f"replace {x.replace("hello",'')}")
error
print(f"replace {x.replace("hello",'')}")
^
SyntaxError: f-string: unmatched '('
Try this instead
x = 'hellothere'
print(f"replace {x.replace('hello','')}")
single quotes 'hello'
output is
replace there
Your problem is double quotes inside double quotes.
For example,
- OK –>
f"hello ' this is good"
- OK –>
f'hello " this is good'
- ERROR –>
f"hello " this breaks"
- ERROR –>
f'hello ' this breaks'
This one should work correctly:
print(f"{index+1}. {value[-1].replace('[Gmail]/', '')}")
Out of scope but still I do not advise you to use replace
inside f-string
. I think that it would be better to move it to a temp variable.
Another way to do some string formatting (which in my opinion improves readability) :
print("{0}. {1}".format(index+1,
value[-1].replace("[Gmail]/", "")))
I had the same issue,change all the double quote within the parenthesis to single quotes.It should work
eg from
print( f" Water : {resources["water"] } " )
to
print( f" Water : {resources[‘water’] } " )
I’m trying to use f-strings in python to substitute some variables into a string that I’m printing, and I’m getting a syntax error.
Here’s my code:
print(f"{index+1}. {value[-1].replace("[Gmail]/", '')}")
I only started having the problem after I added the replace. I’ve checked plenty of times and I’m certain that I’m not missing a parenthesis. I know that there are plenty of other ways to accomplish this, some of which are probably better, but I’m curious why this doesn’t work.
Seems like this does not work
x = 'hellothere'
print(f"replace {x.replace("hello",'')}")
error
print(f"replace {x.replace("hello",'')}")
^
SyntaxError: f-string: unmatched '('
Try this instead
x = 'hellothere'
print(f"replace {x.replace('hello','')}")
single quotes 'hello'
output is
replace there
Your problem is double quotes inside double quotes.
For example,
- OK –>
f"hello ' this is good"
- OK –>
f'hello " this is good'
- ERROR –>
f"hello " this breaks"
- ERROR –>
f'hello ' this breaks'
This one should work correctly:
print(f"{index+1}. {value[-1].replace('[Gmail]/', '')}")
Out of scope but still I do not advise you to use replace
inside f-string
. I think that it would be better to move it to a temp variable.
Another way to do some string formatting (which in my opinion improves readability) :
print("{0}. {1}".format(index+1,
value[-1].replace("[Gmail]/", "")))
I had the same issue,change all the double quote within the parenthesis to single quotes.It should work
eg from
print( f" Water : {resources["water"] } " )
to
print( f" Water : {resources[‘water’] } " )