Python definition String

Source: Internet
Author: User

What if the string contains ‘ and contains both " ?

At this point, you need to "escape" some special characters of the string, and the Python string is \ escaped.

To represent a stringBob said "I‘m OK".
Because ' and ' can cause ambiguity, so we insert a representation before it that \ this is an ordinary character and does not represent the beginning of the string, so the string can be represented as

' Bob said \ ' i\ ' m ok\ '. '

Note: The escape character \ does not count toward the contents of the string.

Second, if a string contains a lot of characters that need to be escaped, it would be cumbersome to escape each character, this time you can prefix the string with R, indicating that this is a raw string, the inside of the string will not need to escape.

For example: R ' \ (~_~)/\ (~_~)/'. However, the r‘...‘ notation cannot represent a multiline string, nor can it represent ‘ " a string containing and.

If you want to represent multiple lines of string, you can use the expression ' ... '.

For example: "' Line1

Line2

Line3 ', the notation is the same as ' Line1\nline2\nline3 '.

You can also add R in front of a multiline string.

C. To print Chinese, you must use Unicode. such as: U ' China '.

If it is a multi-line Chinese string, use ur

ur ' python's Unicode string supports "Chinese", "Japanese", "Korean" and many other languages "

Attention:

If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line #-*-Coding:utf-8-*-

easy to mistake: if the the code edits the first line of comments #-*-coding:utf-8-*-, indicating that the following Python code uses UTF-8 encoding, and if you add U ' "..." in the string, this will cause the interpreter to use Unicode encoded output, resulting in decoding errors. We should remember how the encoding should be decoded in any way, otherwise there is a good chance of coding errors.

Python definition String

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.