b String with several inputs (TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str')


I am trying to code a String with several inputs, but this String should be converted into bytes because I am doing a program that connects to a server (using sockets), that uses a protocol which needs to receive messages in bytes.
I am doing the following code:

print("Please introduce your username: ")
username = input()
print("Please introduce your password: ")
password = input()
client_socket.send(b"AUTH:%s:%sn"%(username, password)) 

It gives me the following error:

%b requires a bytes-like object, or an object that implements ____bytes____, not 'str'

The server should receive the following message:


with the appropiate username and password in order to log in.

Do you have any idea about how to make this work?

Asked By: solrakcs




username = input()
password = input()
data = "AUTH:{}:{}n".format(username, password)


  1. Obtain the data you need
  2. Use string formatting to build the string you require1.
  3. Use the build-int encode method of strings to obtain a byte object2.
  4. Profit!

1You can also use python’s f-string or python’s printf-style formatting to build the string you need.

2 The default encoding is UTF-8, which gives you access to the entire Unicode character set. You can gain more insight about string encoding in the Unicode HOWTO.

Answered By: pablotrinidad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.