Solve the Problem of concatenating strings and numbers in Python.
Preface
As we all know, Python does not automatically convert the type when connecting strings in a weak type language such as JS or PHP. If you splice strings and numbers directly, an error is reported.
See the following code:
# Coding = utf8str = 'your score is: 'num = 82 text = str + num + '| qiongtai blog 'print text
Execution result
Direct error: TypeError: cannot concatenate 'str' and 'int' objects
To solve this problem, you only need to convert num to the string type in advance. You can use the bytes function to convert int to the string type.
Code:
# Coding = utf8str = 'your score is: 'num = 82num = bytes (num) text = str + num +' | qiongtai blog 'print text
Result:
Summary
The preceding content solves all the errors reported by concatenating strings and numbers in Python. I hope this article will be helpful for you to learn and use python. If you have any questions, please leave a message.