2015年1月25日 星期日

Exercise 10: What Was That?

Exercise 10: What Was That?
In Exercise 9 I threw you some new stuff, just to keep you on your toes. I showed you two ways to make a string that goes across multiple lines. In the first way, I put the characters \n (backslash n) between the names of the months. These two characters put a new line character into the string at that point.
This \ (backslash) character encodes difficult-to-type characters into a string. There are various "escape sequences" available for different characters you might want to use. We'll try a few of these sequences so you can see what I mean.
An important escape sequence is to escape a single-quote ' or double-quote ". Imagine you have a string that uses double-quotes and you want to put a double-quote inside the string. If you write "I "understand" joe." then Python will get confused because it will think the " around "understand" actually ends the string. You need a way to tell Python that the " inside the string isn't a real double-quote.
To solve this problem you escape double-quotes and single-quotes so Python knows to include in the string. Here's an example:
"I am 6'2\" tall."  # escape double-quote inside string
'I am 6\'2" tall.'  # escape single-quote inside string
The second way is by using triple-quotes, which is just """ and works like a string, but you also can put as many lines of text as you want until you type """ again. We'll also play with these.


tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print (tabby_cat)
print (persian_cat)
print (backslash_cat)
print (fat_cat)

>>> ============ RESTART =============
>>> 
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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

What You Should See

Look for the tab characters that you made. In this exercise the spacing is important to get right.
$ python ex10.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

Escape Sequences

This all of the escape sequences Python supports. You may not use many of these, but memorize their format and what they do anyway. Try them out in some strings to see if you can make them work.
ESCAPEWHAT IT DOES.
\\Backslash ()
\'Single-quote (')
\"Double-quote (")
\aASCII bell (BEL)
\bASCII backspace (BS)
\fASCII formfeed (FF)
\nASCII linefeed (LF)
\N{name}Character named name in the Unicode database (Unicode only)
\r ASCIICarriage Return (CR)
\t ASCIIHorizontal Tab (TAB)
\uxxxxCharacter with 16-bit hex value xxxx (Unicode only)
\UxxxxxxxxCharacter with 32-bit hex value xxxxxxxx (Unicode only)
\vASCII vertical tab (VT)
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh
Here's a tiny piece of fun code to try out:
while True:
    for i in ["/","-","|","\\","|"]:
        print "%s\r" % i,

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...