Skip to content

Commit 803b9e2

Browse files
committed
string 2
1 parent d2c4918 commit 803b9e2

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

107.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,40 @@
1212

1313
>这两个都是python的内建函数(built-in function)。关于python的内建函数,下面这个表格都列出来了。所谓内建函数,就是能够在python中直接调用,不需要做其它的操作。
1414
15-
| | |Built-in Functions| | |
16-
|--|--|----------------|--|--|
15+
Built-in Functions
16+
--------------------------------------------------------------
1717
|abs() | divmod() | input()| open()| staticmethod()|
18+
--------------------------------------------------------------
1819
|all() | enumerate() | int() | ord() | str()|
20+
--------------------------------------------------------------
1921
|any() | eval() | isinstance()| pow()| sum()|
22+
--------------------------------------------------------------
2023
|basestring() | execfile() | issubclass() | print() | super()|
24+
--------------------------------------------------------------
2125
|bin() | file() | iter()| property()| tuple()|
26+
--------------------------------------------------------------
2227
|bool() | filter() | len() | range() | type()|
28+
--------------------------------------------------------------
2329
|bytearray() | float()| list() | raw_input()| unichr()|
30+
--------------------------------------------------------------
2431
|callable() | format() | locals() | reduce() | unicode()|
32+
--------------------------------------------------------------
2533
|chr() | frozenset() | long() | reload() | vars()|
34+
--------------------------------------------------------------
2635
|classmethod()| getattr()| map() | repr() | xrange()|
36+
--------------------------------------------------------------
2737
|cmp() | globals()| max()| reversed()| zip()|
38+
--------------------------------------------------------------
2839
|compile() |hasattr() | memoryview()| round() | __import__()|
40+
--------------------------------------------------------------
2941
|complex() |hash() | min()| set() | apply()|
42+
--------------------------------------------------------------
3043
|delattr() |help()| next()| setattr()| buffer()|
44+
--------------------------------------------------------------
3145
|dict() | hex() |object() |slice() | coerce()|
46+
--------------------------------------------------------------
3247
|dir() | id() |oct() |sorted() |intern()|
48+
--------------------------------------------------------------
3349

3450
这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?
3551

@@ -136,6 +152,40 @@
136152

137153
这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请看官调试一下,如果没有通过,仔细看报错信息,你能够从中获得修改方向的信息。
138154

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+
这种方法在做网站设置网站目录结构的时候非常有用。使用了原始字符串,就不需要转义了。
139189
------
140190

141191
[总目录](./index.md)   |   [上节:字符串(1)](./106.md)   |   [下节:字符串(3)](./108.md)

index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
4. [常用数学函数和运算优先级](./104.md)==>math模块,求绝对值,运算优先级
1919
5. [写一个简单程序](./105.md)==>程序和语句,注释
2020
6. [字符串(1)](./106.md)==>字符串定义,转义符,字符串拼接,str()与repr()区别
21-
7. [字符串(2)](./107.md)==>raw_input,print,内建函数,再做一个小程序
21+
7. [字符串(2)](./107.md)==>raw_input,print,内建函数,原始字符串,再做一个小程序
2222
8. [字符串(3)](./108.md)
2323
3. 与运算有关的函数
2424
3. 字符串

0 commit comments

Comments
 (0)