|
12 | 12 |
|
13 | 13 | >这两个都是python的内建函数(built-in function)。关于python的内建函数,下面这个表格都列出来了。所谓内建函数,就是能够在python中直接调用,不需要做其它的操作。
|
14 | 14 |
|
15 |
| -| | |Built-in Functions| | | |
16 |
| -|--|--|----------------|--|--| |
| 15 | +Built-in Functions |
| 16 | + -------------------------------------------------------------- |
17 | 17 | |abs() | divmod() | input()| open()| staticmethod()|
|
| 18 | +-------------------------------------------------------------- |
18 | 19 | |all() | enumerate() | int() | ord() | str()|
|
| 20 | +-------------------------------------------------------------- |
19 | 21 | |any() | eval() | isinstance()| pow()| sum()|
|
| 22 | +-------------------------------------------------------------- |
20 | 23 | |basestring() | execfile() | issubclass() | print() | super()|
|
| 24 | +-------------------------------------------------------------- |
21 | 25 | |bin() | file() | iter()| property()| tuple()|
|
| 26 | +-------------------------------------------------------------- |
22 | 27 | |bool() | filter() | len() | range() | type()|
|
| 28 | +-------------------------------------------------------------- |
23 | 29 | |bytearray() | float()| list() | raw_input()| unichr()|
|
| 30 | +-------------------------------------------------------------- |
24 | 31 | |callable() | format() | locals() | reduce() | unicode()|
|
| 32 | +-------------------------------------------------------------- |
25 | 33 | |chr() | frozenset() | long() | reload() | vars()|
|
| 34 | +-------------------------------------------------------------- |
26 | 35 | |classmethod()| getattr()| map() | repr() | xrange()|
|
| 36 | +-------------------------------------------------------------- |
27 | 37 | |cmp() | globals()| max()| reversed()| zip()|
|
| 38 | +-------------------------------------------------------------- |
28 | 39 | |compile() |hasattr() | memoryview()| round() | __import__()|
|
| 40 | +-------------------------------------------------------------- |
29 | 41 | |complex() |hash() | min()| set() | apply()|
|
| 42 | +-------------------------------------------------------------- |
30 | 43 | |delattr() |help()| next()| setattr()| buffer()|
|
| 44 | +-------------------------------------------------------------- |
31 | 45 | |dict() | hex() |object() |slice() | coerce()|
|
| 46 | +-------------------------------------------------------------- |
32 | 47 | |dir() | id() |oct() |sorted() |intern()|
|
| 48 | +-------------------------------------------------------------- |
33 | 49 |
|
34 | 50 | 这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?
|
35 | 51 |
|
|
136 | 152 |
|
137 | 153 | 这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请看官调试一下,如果没有通过,仔细看报错信息,你能够从中获得修改方向的信息。
|
138 | 154 |
|
| 155 | +##原始字符串 |
| 156 | + |
| 157 | +所谓原始字符串,就是指字符串里面的每个字符都是原始含义,比如反斜杠,不会被看做转义符。如果在一般字符串中,比如 |
| 158 | + |
| 159 | + >>> print "I like \npython" |
| 160 | + I like |
| 161 | + python |
| 162 | + |
| 163 | +这里的反斜杠就不是“反斜杠”的原始符号含义,而是和后面的n一起表示换行(转义了)。当然,这似乎没有什么太大影响,但有的时候,可能会出现问题,比如打印DOS路径(DOS,有没有搞错,现在还有人用吗?) |
| 164 | + |
| 165 | + >>> dos = "c:\news" |
| 166 | + >>> dos |
| 167 | + 'c:\news' #这里貌似没有什么问题 |
| 168 | + >>> print dos #当用print来打印这个字符串的时候,就出问题了。 |
| 169 | + c: |
| 170 | + ews |
| 171 | + |
| 172 | +如何避免?用前面讲过的转义符可以解决: |
| 173 | + |
| 174 | + >>> dos = "c:\\news" |
| 175 | + >>> print dos |
| 176 | + c:\news |
| 177 | + |
| 178 | +此外,还有一种方法,如: |
| 179 | + |
| 180 | + >>> dos = r"c:\news" |
| 181 | + >>> print dos |
| 182 | + c:\news |
| 183 | + >>> print r"c:\news\python" |
| 184 | + c:\news\python |
| 185 | + |
| 186 | +状如`r"c:\news"`,由r开头引起的字符串,就是原始字符串,在里面放任何字符都表示该字符的原始含义。 |
| 187 | + |
| 188 | +这种方法在做网站设置网站目录结构的时候非常有用。使用了原始字符串,就不需要转义了。 |
139 | 189 | ------
|
140 | 190 |
|
141 | 191 | [总目录](./index.md) | [上节:字符串(1)](./106.md) | [下节:字符串(3)](./108.md)
|
|
0 commit comments