Solve the Problem of concatenating strings and numbers in Python.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.