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

2024-09-26 18:00:17 发布

您现在位置:Python中文网/ 问答频道 /正文

在我当前的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运行这个命令时,它返回

credits to文本

www.example.com

我希望它能以同样的方式返回

credits to text

www.example.com

一切都很好,我只是希望格式都匹配。先谢谢你


Tags: oftooutputwww格式linkallaudio
2条回答

我误解了这个问题。如果您也希望actualoutput上的格式,可以将反勾号移到末尾

因此,应该是:

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

结果是:credits to <actualoutput>

在换行符之前,将结束回勾移动到字符串的末尾,如下所示:

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

相关问题 更多 >

    热门问题