Python 如何将字符串插入较大字符串的特定部分?
我正在尝试编写一个小程序,自动生成许多URL,每个URL基本相同,只是有一个不同之处,就是在每个URL中插入了不同的外语缩写 例如,我有以下代码:Python 如何将字符串插入较大字符串的特定部分?,python,string,Python,String,我正在尝试编写一个小程序,自动生成许多URL,每个URL基本相同,只是有一个不同之处,就是在每个URL中插入了不同的外语缩写 例如,我有以下代码: base_url = 'https://habitica.fandom.com/wiki/Special:WhatLinksHere' wiki_page = '/File:Gold.png' languages = ['da', 'de', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt-br', 'ru', 'tr
base_url = 'https://habitica.fandom.com/wiki/Special:WhatLinksHere'
wiki_page = '/File:Gold.png'
languages = ['da', 'de', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt-br', 'ru', 'tr', 'zh']
因此,我想在“wiki”之前的位置将这些语言缩写插入base_url值中,从而
https://habitica.fandom.com/da/wiki/Special:WhatLinksHere
https://habitica.fandom.com/de/wiki/Special:WhatLinksHere
https://habitica.fandom.com/es/wiki/Special:WhatLinksHere
等等
我该怎么做呢?是否有一个相当一般的方法,或者我需要进入一些关于字符串的特定文本的非常详细的代码
谢谢!
John你只需制作一个模板
str
ing和format
就可以了
>>> languages
['da', 'de', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt-br', 'ru', 'tr', 'zh']
>>> template = 'https://habitica.fandom.com/{}/wiki/Special:WhatLinksHere'
>>> urls = []
>>> for lang in languages:
... urls.append(template.format(lang))
...
>>> print('\n'.join(urls))
https://habitica.fandom.com/da/wiki/Special:WhatLinksHere
https://habitica.fandom.com/de/wiki/Special:WhatLinksHere
https://habitica.fandom.com/es/wiki/Special:WhatLinksHere
https://habitica.fandom.com/fr/wiki/Special:WhatLinksHere
https://habitica.fandom.com/it/wiki/Special:WhatLinksHere
https://habitica.fandom.com/ja/wiki/Special:WhatLinksHere
https://habitica.fandom.com/nl/wiki/Special:WhatLinksHere
https://habitica.fandom.com/pl/wiki/Special:WhatLinksHere
https://habitica.fandom.com/pt-br/wiki/Special:WhatLinksHere
https://habitica.fandom.com/ru/wiki/Special:WhatLinksHere
https://habitica.fandom.com/tr/wiki/Special:WhatLinksHere
https://habitica.fandom.com/zh/wiki/Special:WhatLinksHere
>>>
您只需制作一个模板
str
ing和format
即可
>>> languages
['da', 'de', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt-br', 'ru', 'tr', 'zh']
>>> template = 'https://habitica.fandom.com/{}/wiki/Special:WhatLinksHere'
>>> urls = []
>>> for lang in languages:
... urls.append(template.format(lang))
...
>>> print('\n'.join(urls))
https://habitica.fandom.com/da/wiki/Special:WhatLinksHere
https://habitica.fandom.com/de/wiki/Special:WhatLinksHere
https://habitica.fandom.com/es/wiki/Special:WhatLinksHere
https://habitica.fandom.com/fr/wiki/Special:WhatLinksHere
https://habitica.fandom.com/it/wiki/Special:WhatLinksHere
https://habitica.fandom.com/ja/wiki/Special:WhatLinksHere
https://habitica.fandom.com/nl/wiki/Special:WhatLinksHere
https://habitica.fandom.com/pl/wiki/Special:WhatLinksHere
https://habitica.fandom.com/pt-br/wiki/Special:WhatLinksHere
https://habitica.fandom.com/ru/wiki/Special:WhatLinksHere
https://habitica.fandom.com/tr/wiki/Special:WhatLinksHere
https://habitica.fandom.com/zh/wiki/Special:WhatLinksHere
>>>
仅供参考,这里应用的概念叫做“啊,是的,不久前我刚刚读到了关于字符串格式化的各种方法,我甚至没有意识到这是我所需要的!事实上,使用f-strings会更好吗?在这种情况下,最好使用
str.format
而不是f-strings
FYI。这里应用的概念叫“啊,是的,我不久前刚刚读过关于字符串格式化的各种方法,我甚至没有意识到这是我所需要的!事实上,使用f-strings会更好吗?在这种情况下,最好使用str.format
而不是f-strings
Related:Related: