笨办法学python第10课

2018-09-03  本文已影响9人  3ab9db71c244

10《What Was That?》

tabby_cat ="\ti'm tabbed in."  #\t首行缩进
persian_cat = "i'm split\n on a line."  #\n换行
backslash_cat = "i'm \\ a \\ cat."  #\\必须同时是两个\\,只要有一个是\,全部都是\
fat_cat = """  
i'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""   #\t  缩进
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)

运行结果:
/Users/tongshiba/PycharmProjects/ex3/test10.py
    i'm tabbed in.
i'm split
 on a line.
i'm \ a \ cat.
  
i'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass

练习题:
1. Memorize all the escape sequences by putting them on flash cards.

Escape What it does.
\\ Backslash (\)
\' Single-quote (')
\" Double-quote (")
\a ASCII bell (BEL)
\b ASCII backspace (BS)
\f ASCII formfeed (FF)
\n ASCII linefeed (LF)
\N{name} Character named name in the Unicode database (Unicode only)
\r Carriage return (CR)
\t Horizontal tab (TAB)
\uxxxx Character with 16-bit hex value xxxx
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx
\v ASCII vertical tab (VT)
\000 Character with octal value 000
\xhh Character with hex value h

2. Use ''' (triple-single-quote) instead. Can you see why you might use that instead of """?

多行

3. Combine escape sequences and format strings to create a more complex format。

not yet


Common Student Questions 常见问题:

1.
I still haven’t completely figured out the last exercise. Should I continue? 

Yes, keep going. Instead of
stopping, take notes listing things you don’t understand for each exercise. Periodically go
through your notes and see if you can figure these things out after you’ve completed more
exercises. Sometimes, though, you may need to go back a few exercises and do them again.

2.
What makes \\ special compared to the other ones? 

It’s simply the way you would write out one
backslash (\) character. Think about why you would need this.

3.
When I write // or /n it doesn’t work. 

That’s because you are using a forward-slash (/) and not a backslash (\). They are different characters that do very different things.

4. I don’t get Study Drill 3. What do you mean by “combine” escape sequences and formats?

One concept I need you to understand is that each of these exercises can be combined to solve problems. Take what you know about format strings and write some new code that uses format strings and the escape sequences from this exercise.

5.What’s better, ''' or """? 

It’s entirely based on style. Go with the ''' (triple-single-quote) style for now, but be ready to use either depending on what feels best or what everyone else is doing


上一篇下一篇

猜你喜欢

热点阅读