在 discord.py 中格式化字符串变量

Formatting a string variable in discord.py

在我当前的 discord 机器人中,我希望我的程序以相同的格式输出所有内容:

@client.command()
async def audios(ctx):
    # audio is a link from a list of links i have stored in all_audios
    audio = random.choice(all_audios)
    audiostr = str(audio)
    # this queries a part of the link
    output = PurePosixPath(
        unquote(
            urlparse(audiostr).path
        )     
    ).parts[1]
    actualoutput = str(output)
    await ctx.send("`credits to `" + actualoutput + "\n" + audio) 

是否可以使用反引号 actualoutput 进行格式化? 当我通过 discord 运行 这个命令时,它 returns

credits to 文字

www.example.com

我希望它 return 为

credits to text

www.example.com

一切正常,我希望格式能够全部匹配。提前谢谢你。

我误解了这个问题。如果你也想要 actualoutput 上的格式,你可以将他的反引号移到最后。

所以应该是:

await ctx.send("`credits to " + actualoutput + "`\n" + audio) 

结果为:credits to <actualoutput>

将结束反引号移动到换行符之前的字符串末尾,如下所示:

await ctx.send("`credits to " + actualoutput + "`\n" + audio)