Issue
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:
‘fhttps://www.amazon.com//Best-Sellers-Amazon-Launchpad/zgbs/boost/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}’
However, the letter f should be outside of the quotation marks surrounding the url. That is, I am seeking the following:
f’https://www.amazon.com/Best-Sellers-Electronics/zgbs/electronics/ref=zg_bs_pg_{pageNo}?_encoding=UTF8&pg={pageNo}’
Solution
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).
-
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
-
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
-
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)