How do I merge values into one message in discord.py

Viewed 179

I want my bot to be able to say a message, mention a user, then respond with a random message, all in the same line, as opposed to having it in 3 separate lines.

This was my previous method:

if msg.startswith('respond')
    await message.channel.send('alright then,')
    await message.channel.send(message.author.mention)
    await message.channel.send(random.choice(responses))

However, this makes them all appear in the same line, but I can't figure out how to merge them into a singular line. This was one of my many failed attempts:

if msg.startswith('respond'):
    await message.channel.send ('alright then, <message.author.mention> <random.choice(responses)>')

(please don't make fun of my amateur coding skills lmao)

2 Answers

assuming you are using python3+, you could do f-strings

if msg.startswith('respond'):
    await message.channel.send(f'alright then, {message.author.mention} {random.choice(responses)}')

there are other alternatives to this, such as format strings

if msg.startswith('respond'):
    await message.channel.send('alright then, {} {}'.format(message.author.mention, random.choice(responses))

concatenation methods like

if msg.startswith('respond'):
    await message.channel.send('alright then, ' + message.author.mention + ' ' + random.choice(responses))

f-string

Inzer Lee answer is correct, but in Python 3.8+ the PEP documentation suggests to do concatenate strings using f-string method:
if msg.startswith("respond"):
    await message.channel.send(f"alright then, {message.author.mention} {random.choice(responses)}")

PEP-8

So, if you want to respect the PEP-8 indentation standard, this code is longer than 72 characters, and you should do like this:
await message.channel.send("alright then,"
    f"{message.author.mention} {random.choice(responses)}"
)
Related