如何正确使用strip()类型?

2024-10-02 00:43:57 发布

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

我目前正在尝试制作一个不和谐机器人。我正在尝试设置它,以便它在控制台中发送一条消息,该消息是(用户)在(通道)中发送的(命令)。按照ctx的工作方式,字符串总是有一个前导和尾随空格。我想知道如何删除所述空白。我试过使用.strip,如你所见,但它根本不起作用

def receivedcommand(ctx):
    print(current_datetime,'INFO: Chat Command Received;',ctx.author.name,'sent \"',ctx.message.content.strip(),'\" in \"#',ctx.channel.name.strip()[2:],'\"')

[2:]用于删除我的服务器中的前缀,在本例中是一个表情符号和一个不协调兼容管道

该函数返回

[07/16/21 21:03:39] INFO: Chat Command Received; Vyladence sent " |parrot awa2 " in "# programming "

在#编程中接收Vyladence发送的|parrot命令时。有人知道我搞砸了什么吗


Tags: 用户namein命令info消息chat机器人
2条回答

你确实剥夺了所有可以剥夺的空间print本身正在"ctx.message.content.strip()的值之间添加空格

不要将多个参数传递给^ {},考虑建立一个字符串:

print(f'{current_datetime} INFO: Chat Command Received; ctx.author.name sent "{ctx.message.content.strip()}" in "#{ctx.channel.name.strip()[2:]}"')

实际上,您已将其剥离,因为您使用的是逗号,它会自动添加一个空格分隔符,若要修复它,请尝试以下操作:

print(current_datetime,'INFO: Chat Command Received;',ctx.author.name,'sent \"',ctx.message.content.strip(),'\" in \"#',ctx.channel.name.strip()[2:],'\"', sep='')

例如:

使用逗号打印会自动添加空格分隔符,如下所示:

>>> print('a', 'b')
a b
>>> 

添加sep=''(将分隔符分配给空字符串)可以:

>>> print('a', 'b', sep='')
ab
>>> 

相关问题 更多 >

    热门问题