Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve TypeError: can only concatenate str (not "int") to str [duplicate]

Tags:

python

unicode

  • I decided to make some kind of secret code for testing purposes with Unicode.
  • I've done that by adding numbers to Unicode so it would be kind of secret.
  • I've been getting this error, but I don't know how to solve it.
    • Is there any solution?

Original Code

message = input("Enter a message you want to be revealed: ") secret_string = "" for char in message:     secret_string += str(chr(char + 7429146)) print("Revealed", secret_string) q = input("") 

Original Error

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-182-49ece294a581> in <module>       2 secret_string = ""       3 for char in message: ----> 4     secret_string += str(chr(char + 7429146))       5 print("Revealed", secret_string)       6 q = input("")  TypeError: can only concatenate str (not "int") to str 

Updated code

while True:     try:         message = int(input("Enter a message you want to be decrypt: "))         break     except ValueError:         print("Error, it must be an integer") secret_string = "" for char in message:     secret_string += chr(ord(char - str(742146))) print("Decrypted", secret_string) q = input("") 
like image 248
9ae Avatar asked Jul 09 '18 19:07

9ae


People also ask

How do you fix TypeError can only concatenate str not int to str?

The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.

Can only concatenate str not TypeError to STR?

The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.

Why can't Python concatenate str and int objects?

In Python, we cannot concatenate a string and an integer together. They have a different base and memory space as they are completely different data structures.


2 Answers

Python working a bit differently to JavaScript for example, the value you are concatenating needs to be same type, both int or str...

So for example the code below throw an error:

print( "Alireza" + 1980) 

like this:

Traceback (most recent call last):   File "<pyshell#12>", line 1, in <module>     print( "Alireza" + 1980) TypeError: can only concatenate str (not "int") to str 

To solve the issue, just add str to your number or value like:

print( "Alireza" + str(1980)) 

And the result as:

Alireza1980 
like image 73
Alireza Avatar answered Sep 22 '22 02:09

Alireza


instead of using " + " operator

print( "Alireza" + 1980) 

Use comma " , " operator

print( "Alireza" , 1980) 
like image 43
Abhishek Kashyap Avatar answered Sep 20 '22 02:09

Abhishek Kashyap