Buğra İşgüzar
Buğra İşgüzar

Reputation: 37

Can't convert 'int' object to str

I have the following code that causes the below error.

elif args[0]=="online":
    onlines = zxLoLBoT.get_friends_online(self)
    self.message(sender, "Toplam "+len(onlines)+" kişi açık.")

error ss

Upvotes: 1

Views: 148

Answers (2)

Or Duan
Or Duan

Reputation: 13810

I would use .format() to do it:

self.message(sender, "Toplam {} kişi açık.".format(len(onlines)))

This way you do not need to use extra code to convert int to str.

Upvotes: 4

Tanveer Alam
Tanveer Alam

Reputation: 5275

self.message(sender, "Toplam " + str(len(onlines))+ " kişi açık.")

You were trying to concatenate a string with an integer.

The built-in function len() will always return an integer type, so you must convert it to a string with str() when concatenating it another string.

len(...)
    len(object)

    Return the number of items of a sequence or collection.

Upvotes: 3

Related Questions