python格式的字符串内字符串内字符串

python格式的字符串内字符串内字符串,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我想把字符串放在字符串中,如下所示 f"{home.select_one('td a[title="Odds"]')['href']}" 但它返回错误 SyntaxError: invalid syntax 转义引号: print("{home.select_one('td a[title=\"Odds\"]')['href']}") 使用.format In [117]: home = BeautifulSo

我想把字符串放在字符串中,如下所示

f"{home.select_one('td a[title="Odds"]')['href']}"
但它返回错误

SyntaxError: invalid syntax
转义引号:

print("{home.select_one('td a[title=\"Odds\"]')['href']}")

使用
.format

In [117]: home = BeautifulSoup("""<td><a title="Odds" href="https://url"></td>""", "html.parser")

In [118]: "{}".format(home.select_one('td a[title=\"Odds\"]')['href'])
Out[118]: 'https://url'

哪一个是变量名?使用三重引号。变量名:home我在原始线程@RavanelliSyntaxError:f-string表达式部分中回答了这个问题,不能包含反斜杠。我没有提到f-string,这是用于一般和原始的strings@Ravanelli:不是,不是。Wasif提供的线路正常工作。因此,你在做别的事情。
In [123]: f"""
     ...: {home.select_one('td a[title="Odds"]')['href']}
     ...:
     ...: """.strip()
Out[123]: 'https://url'