DEV Community

Cover image for Type error in python
Abu zubaer
Abu zubaer

Posted on

Type error in python

in python, You can not concatenate two types of data.
suppose you are want to concatenate an integer value to a string value.
string = "hello earth"
number = 404
print(string + 'how are you' + 'your server error number is' + number)
the result will be a type error

now, you think about how to concatenate different types of data !!
in python, have a simple way to solve this problem.
you can solve this problem by converting the data.
suppose, you have two types of data number and string
number = 14644
string = 'anything'
convert_number_as_a_string = str(number) //converting number to string by str method.
print('hey number' + number + 'hey string' + string) //wow it's work now

but I think this is the weird way.
there has another easiest way to solve this problem.
f-string can solve this problem

number = 11544
string = 'anything'
result = f"its number {number}. its string {string}"
print(result)

woow..it's work.
can't believe it?
just open your code editor or ide and try this / practicing this.

this is my first post in dev. pardon me, if I did any mistake.
note -- English is not my native language so normally you've could get a grammatical error.

tanks for reading..

Top comments (0)