Methods for solving errors in string and digital stitching in Python

Source: Internet
Author: User
Objective

It is well known that Python is not like JS or PHP in the weakly typed language when strings are connected automatically convert the type, if the string and the number of direct concatenation will directly error.

As in the following code:

# coding=utf8str = ' Your score is: ' num = 82text = str+num+ ' points | Joan Taiwan Blog ' Print text '

Execution results

Direct error: Typeerror:cannot concatenate ' str ' and ' int ' objects

To resolve this method, you can use the bytes function to convert the int to string type only if you have to convert num to character strings in advance.

Code:

# coding=utf8str = ' Your score is: ' num = 82num = bytes (num) Text = str+num+ ' points | Joan Taiwan Blog ' Print text '

The results are done:

Summarize

The above is to solve the Python string and the number of errors in the concatenation of the entire content, I hope that the content of this article for you to learn or use Python can help, if there are questions you can message exchange.

  • 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.