【问题标题】:Add hyperlink to a text python将超链接添加到文本python
【发布时间】:2023-01-11 04:27:16
【问题描述】:

你好,我有一个链接和一个文本。我想超链接这个文本。

例如:

我尝试了下面的一个。但它没有以超链接格式添加新的文本链接。

new_test='www.youtube.com/'
hyperlink_format= '<a href=new_test>text of the link</a>'

打印时我得到的输出hyperlink_format

&lt;a href=new_test&gt;text of the link&lt;/a&gt;

我的用例是它应该打印

'&lt;a href=www.youtube.com/&gt;text of the link&lt;/a&gt;'

我也尝试过使用 `<a href='{}{}'>{}'.format()' 但这需要两个参数,所以不能继续吗?还有其他想法吗? 谢谢!

【问题讨论】:

  • 您确实有两个参数:URL 和文本"&lt;a href='{}'&gt;{}&lt;/a&gt;".format("www.youtube.com","text of the link")
  • 你想做什么?如果您想插入指向&lt;a&gt; 标签的链接,请使用f'&lt;a href="https://{new_test}"&gt;text of the link&lt;/a&gt;'
  • 不,我每次都提供更改的链接,因此它不应该是动态的
  • 你的意思是“它应该是动态的”。

标签: python href


【解决方案1】:

对于第一种方法,您应该将参数包含在大括号中,例如 &lt;a href={new_test}&gt;text of the link&lt;/a&gt;。而对于第二种方法,你还没有为格式提供参数,它应该是这样的:&lt;a href={}&gt;text&lt;/a&gt;'.format(new_test)

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 2013-03-03
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多