Python original string, python string

Source: Internet
Author: User

Python original string, python string

1 str1 = 'let's go '2 File "<input>", line 13 str1 = 'let's go '4 ^ 5 SyntaxError: invalid syntax

Because str1 contains "'", the compiler cannot escape and report an error. The correct code is as follows:

Str1 = 'let \'s go 'str1 "let's go"

 

Use \ as the escape character to escape the characters in the string

1 str2 = 'C: \ now '2 str23' C: \ now '4 print (str2) 5 C: 6 ow

The \ n in str2 is the Escape Character of the line break, so the output format is new. You can use the original string Escape Character r.

Str2 = r 'C: \ now 'print (str2) C: \ now

However, the backslash cannot be at the end of the string.

Str2 = r'c: now \ 'file "<input>", line 1 str2 = r'c: now \ '^

In this way, the compiler considers \ as an escape character escape, which can be written in this way.

1 str2 = r 'C: \ now '+' \ '2 print (str2) 3 C: \ now \

 

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.