Invalid Syntax When F' String Dictionary
When I try to f'string dictionary, it says invalid syntax. def up_low(s): d={'upper':0,'lower':0} for c in s: if c.isupper(): d['upper']+=1
Solution 1:
Don't use "
in a string enclosed with "
, either use '
, or enclose the string in '
:
f"No. of Upper case characters: {d['upper']}"
Or:
f'No. of Upper case characters: {d["upper"]}'
Solution 2:
Change your double quotes to single quotes inside {d["upper"]}
Here is full working code:
defup_low(s):
d={"upper":0,"lower":0}
for c in s:
if c.isupper():
d["upper"]+=1print (f"No. of Upper case characters: {d['upper']}")
elif c.islower():
d["lower"]+=1print (f"No. of Lower case characters:{d['lower']}")
else:
pass
Solution 3:
You can use any "other" string delimiter - even the triple ones:
d = {"k":"hello"}
print(f'''{d["k"]}''')
print(f"""{d["k"]}""")
print(f'{d["k"]}')
print(f"{d['k']}")
Output:
hello
hello
hello
hello
Using the same ones confuses python - it does not know where the f-string ends and your key-string starts if you use the same delimiters.
Post a Comment for "Invalid Syntax When F' String Dictionary"