Stupid way to learn Python (10)

Source: Internet
Author: User

Exercise 10: What is that?

In Exercise 9, I have you in touch with something new. I let you see two ways to extend a string to multiple lines. The first method is to separate the months by \ n (back-slash N). The purpose of these two characters is to put a "new line" character in that position.

Use backslashes \ (Back-slash) to place characters that are difficult to print into strings. There are many such so-called escape sequences for different symbols (escape sequences), but there is a special escape sequence, which is a double backslash (doubles back-slash) \ \. The combination of these two characters prints a backslash. Let's do a few exercises and then you'll know the meaning of these escape sequences.

Another important escape sequence is to escape single quotes ' and double quotes '. Imagine you have a string quoted in double quotes, you want to add a set of double quotes in the contents of the string, for example, you want to say "I" understand "Joe.", Python will assume that the two quotes before and after "understand" are the bounds of the string. So that the string is mistaken. You need a way to tell that the double quotes inside the Python string are not true double quotes.

To solve this problem, you need to escape the double and single quotes, and let Python enclose the quotation marks in the string. Here's an example:

The second method is to use "three quotation marks (triple-quotes)", or "" ", you can put any number of lines of text between a set of three quotation marks. Next you will see the usage.

1Tabby_cat ="\ti ' m tabbed in." 2Persian_cat ="I ' m Split\non a line." 3Backslash_cat ="I ' m \ \ a \ \ Cat." 4 5Fat_cat =""" 6 I ' ll do a list:7 \t* Cat Food8 \t* fishies9 \t* catnip\n\t* GrassTen """  One  A PrintTabby_cat - PrintPersian_cat - PrintBackslash_cat the PrintFat_cat
View Code

Stupid way to learn Python (10)

Related Article

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.