Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot concatenate 'str' and 'instance'

I have a program using GUI elements and returns the error

cannot concatenate 'str' and 'instance' objects

The Code is:

def PeopleSearch():
    query = SearchTerm
    query = ('what is '+ query)
    string = ("<center><font size = 14> " + query + ' </font></center><br><img src =picture')
    j = 0
    try:
        gs = GoogleSearch(query)
        gs.results_per_page = 100
        results = gs.get_results()

The indentations have been changed. Hmm. SearchTerm is basically from a textbox.

like image 726
Moushumi Mitra Avatar asked Jun 30 '13 16:06

Moushumi Mitra


1 Answers

Let me reproduce with a simpler example:

v = 42
query = ('what is ' + v)

You would get:

TypeError: cannot concatenate 'str' and 'int' objects

But now, if you simply call str:

query = ('what is ' + str(v))

That will work. So you only have to make sure str(query) returns what you expect. Be careful, I don't know what kind of object you are manipulating, but you should check if there is any method to get the string representation of it.

Related:

  • TypeError: cannot concatenate 'str' and 'instance' objects (python urllib)
  • Python: TypeError: cannot concatenate 'str' and 'int' objects
  • Cannot concatenate 'str' and 'list' objects
  • TypeError: cannot concatenate 'str' and 'int' objects TypeError: cannot concatenate 'str' and 'type' objects
like image 92
Maxime Chéramy Avatar answered Nov 09 '22 21:11

Maxime Chéramy