parameter substitution in a binary string in python

283 Views Asked by At

Python 3

I want to send a binary string over a socket. I am using

socket.send(b"Hello from {0}",client_id)

however I find the it is not properly substitution clientid.

what am I doing wrong? I do I accomplish this in a binary string

1

There are 1 best solutions below

0
On

{0} is a placeholder for the str.format method, which you should be using, but bytes objects do not have a format method; instead, you can format it as a string first and then convert it to bytes with the encode method:

socket.send("Hello from {0}".format(client_id).encode())