F-Strings Giving Syntaxerror

f-strings giving SyntaxError?

I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here

How to catch a SyntaxError when f-strings are implemented - when obsolete python is used

It looks like all you want to do on pre-3.6 versions is report an error and terminate the program, and you just want a more user-friendly error message than a SyntaxError stack trace. In that case, instead of trying to catch a SyntaxError, have an entry point file that doesn't use f-strings, and do the version check there, before loading any code that uses f-strings:

# entry point
import sys

if sys.version_info < (3, 6):
# report error, terminate program
else:
import whatever
whatever.main()

Getting Error in python SyntaxError: f-string: expressions nested too deeply

I believe just just need to escape the curly brackets to make this work:

f'{{"requests":[{{"indexName":"New_Telemart","params":"query=Mobiles&maxValuesPerFacet=10&page="{i}"&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&facets=%5B%22brand_name%22%2C%22categories%22%2C%22sale_price%22%2C%22total_rating_average%22%2C%22express_delivery%22%5D&tagFilters=&facetFilters=%5B%5B%22categories%3ASmartphones%22%2C%22categories%3AMobile%20%26%20Tablets%22%5D%5D"}},  {{"indexName":"New_Telemart","params":"query=Mobiles&maxValuesPerFacet=10&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&hitsPerPage=1&attributesToRetrieve=%5B%5D&attributesToHighlight=%5B%5D&attributesToSnippet=%5B%5D&tagFilters=&analytics=false&clickAnalytics=false&facets=categories"}}]}}'

You can read the f-string documentation for more information

Getting an invalid syntax when trying to perform string interpolation

As suggested by Josh Lee in the comment section, that kind of string interpolation was added in Python 3.6 only, see What’s New In Python 3.6 (here it's called "PEP 498: Formatted string literals").

You, however, seems to be using Python 3.5.2, which does not support that syntax.



Related Topics



Leave a reply



Submit