Vyladence
Vyladence

Reputation: 37

How do I properly use the strip() type?

I'm currently trying to make a discord bot. I am trying to set it so that it will send a message in the console that (user) sent the (command) command in (channel). The way that ctx works, the strings always have a leading and trailing whitespace. I'm wondering how I can remove said whitespace. I tried using .strip, as you can see, but it is not working at all.

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

The [2:] is there to remove the prefix in my server, in this case being an emoji and a discord-compatible pipe.

That function returns

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

upon receiving the |parrot command sent by Vyladence in #programming. Anybody know what I'm messing up here?

Upvotes: 2

Views: 178

Answers (2)

U13-Forward
U13-Forward

Reputation: 71570

You actually stripped it, since you are using a comma, it automatically adds a space delimiter, to fix it try the below:

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

Example:

Printing with comma automatically adds space separators, as you can see below:

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

Adding sep='' (assigning the separator to an empty string) would do it:

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

Upvotes: 3

chepner
chepner

Reputation: 531125

You did strip any space there was to strip. print itself is adding the space between the " and the value of ctx.message.content.strip().

Instead of passing multiple arguments to print, consider building a single string:

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

Upvotes: 1

Related Questions