[SOLVED] In python, how to append a string without quotation marks next to a string with the single quotation marks?


url = https://www.amazon.com//Best-Sellers-Amazon-Launchpad/zgbs/boost/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}

def getData(url):
    new_link = 'f'+ str(url)

###rest of the code

The coding above gives the following output:


However, the letter f should be outside of the quotation marks surrounding the url. That is, I am seeking the following:



Formatted string literals in Python are strings that are prefixed by f or F and whose contents have a special semantics. The f is not a character that is part of the string; you can think of it as a modifier for the string that follows. You cannot construct such a literal by simply concatenating the f character with some string.

There are several ways to achieve your goal. The first (as other answers suggest) uses the simpler approach. If however you insist on separating the URL and dynamically generating and using the formatted string literal, see the second. The third uses a format string (identical to the url in the second).

  1. Use the formatted string literal directly:

    def getData(pageNo):
      new_link = f'https://www.amazon.com/Best-Sellers-Amazon-Launchpad/zgbs/boost/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}'
      # rest of the code
  2. Use eval:

    url = 'https://www.amazon.com/Best-Sellers-Amazon-Launchpad/zgbs/boost/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}'
    def getData(url, pageNo):
       new_link = eval("f'"+ url + "'"))
       # rest of the code
  3. Use .format:

    url = 'https://www.amazon.com/Best-Sellers-Amazon-Launchpad/zgbs/boost/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}'
    def getData(url, pageNo):
       new_link = url.format(pageNo=pageNo)
       # rest of the code

Answered By – nickie

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *