Cod*_*Law 1 python layout multiline multilinestring discord.py
我启动并运行了一个不和谐机器人。我的目标是在执行某个命令后向用户发送消息。这可行,但不知何故我的布局是错误的。我的字符串的第一行是正确的,之后其余的行将带有一些前导空格发送。
client = commands.Bot(command_prefix = '/')
@client.command()
async def start(ctx):
welcome_message = f"""
Welcome to my bot
here is a new line which has leading spaces
this line here has some aswell
"""
await ctx.send(welcome_message)
Run Code Online (Sandbox Code Playgroud)
所以基本上我的消息是这样的:
Welcome to my bot
here is a new line which has leading spaces
this line here has some aswell
Run Code Online (Sandbox Code Playgroud)
即使welcome_message = welcome_message.lstrip()在发送之前也不能解决问题。
在这种情况下,缩进是字符串的一部分(因为它是多行字符串),您可以简单地将其删除:
@client.command()
async def start(ctx):
welcome_message = welcome_message = f"""
Welcome to my bot
here is a new line which has leading spaces
this line here has some aswell
"""
await ctx.send(welcome_message)
Run Code Online (Sandbox Code Playgroud)
如果您想保留缩进,请使用以下textwrap.dedent方法:
import textwrap
@client.command()
async def start(ctx):
welcome_message = welcome_message = f"""
Welcome to my bot
here is a new line which has leading spaces
this line here has some aswell
"""
await ctx.send(textwrap.dedent(welcome_message))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |