Learn Python The Hard Way learning (10)-What is that?

Source: Internet
Author: User

In the 9th exercises, we use two methods to wrap the line. The first method is to add \ n.

Adding different characters After \ can indicate different meanings. If it is "\", it indicates printing \.

The other two important escape sequences are 'and ". If the string you want to print is enclosed by" and contains ", for example," I "understand" joe. ", python will think that" after I "is the end of the string.

To solve this problem, you can do the following:
"I am 6 '2 \" tall ."
'I am 6 \ '2 "tall .'

The second method is to use "", as shown below:
[Python]
1. tabby_cat = "\ tI'm tabbed in ."
2. persian_cat = "I'm split \ non a line ."
3. backslash_cat = "I'm \ a \ cat ."
4.
5.
6. fat_cat = """
7. I'll do a list:
8. \ t * Cat food
9. \ t * Fishies
10. \ t * Catnip \ n \ t * Grass
11 ."""
12.
13.
14. print tabby_cat
15. print persian_cat
16. print backslash_cat
17. print fat_cat

Running result
I'm tabbed in.
I'm split
On a line.
I'm \ a \ cat.

I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass

Root @ he-desktop :~ /Mystuff #

Additional exercise www.2cto.com
1. Search for available escape characters on the Internet.
\ (At the end of a row) indicates that the line is not wrapped.
\ Backslash
\ 'Single quotes
\ "Double quotation marks
\ A bell
\ B Return, indicating to delete the previous character
\ T horizontal Tab
Vertical \ v tab, indicating line feed, and output from \ v.
\ N line feed
\ F form feed

2. Replace "" With ''' to see the effect.
Same role

3. Put escape characters and formatted strings together to create a more complex format.
Persian_cat = "I'm spliton \ v % s line." % ""

4. Do you still remember % r? % R is combined with single quotes and double quotes escape characters and printed out. Compare % r and % s, % r to print the file you want to write, % s to print what you want to see.
Persian_cat = "I \'m spliton \" % s \ "% r." % ("a", "line ")
Output:
I'm spliton "a" 'line '.
Note that line has a single quotation mark, indicating that the program you want to write is a string.

Author: lixiang0522

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.