11#字符串(2)
22
3- ##raw_input和print
4-
5- 自从本课程开始以来,我们还没有感受到computer姑娘的智能。最简单的智能应该体现在哪里呢?想想小孩子刚刚回说话的时候情景吧。
6-
7- > 小孩学说话,是一个模仿的过程,孩子周围的人怎么说,她(他)往往就是重复。看官可以忘记自己当初是怎么学说话了吧?就找个小孩子观察一下吧。最好是自己的孩子。如果没有,就要抓紧了。
8-
9- 通过python能不能实现这个简单的功能呢?当然能,要不然python如何横行天下呀。
10-
11- 不过在写这个功能前,要了解两个函数:raw_input和print
12-
13- > 这两个都是python的内建函数(built-in function)。关于python的内建函数,下面这个表格都列出来了。所谓内建函数,就是能够在python中直接调用,不需要做其它的操作。
14-
15- Built-in Functions
16- --------------------------------------------------------------
17- | abs() | divmod() | input()| open()| staticmethod()|
18- --------------------------------------------------------------
19- | all() | enumerate() | int() | ord() | str()|
20- --------------------------------------------------------------
21- | any() | eval() | isinstance()| pow()| sum()|
22- --------------------------------------------------------------
23- | basestring() | execfile() | issubclass() | print() | super()|
24- --------------------------------------------------------------
25- | bin() | file() | iter()| property()| tuple()|
26- --------------------------------------------------------------
27- | bool() | filter() | len() | range() | type()|
28- --------------------------------------------------------------
29- | bytearray() | float()| list() | raw_input()| unichr()|
30- --------------------------------------------------------------
31- | callable() | format() | locals() | reduce() | unicode()|
32- --------------------------------------------------------------
33- | chr() | frozenset() | long() | reload() | vars()|
34- --------------------------------------------------------------
35- | classmethod()| getattr()| map() | repr() | xrange()|
36- --------------------------------------------------------------
37- | cmp() | globals()| max()| reversed()| zip()|
38- --------------------------------------------------------------
39- | compile() | hasattr() | memoryview()| round() | __ import__ ()|
40- --------------------------------------------------------------
41- | complex() | hash() | min()| set() | apply()|
42- --------------------------------------------------------------
43- | delattr() | help()| next()| setattr()| buffer()|
44- --------------------------------------------------------------
45- | dict() | hex() | object() | slice() | coerce()|
46- --------------------------------------------------------------
47- | dir() | id() | oct() | sorted() | intern()|
48- --------------------------------------------------------------
3+ ##键盘输入
4+
5+ 电脑的智能,一种体现就是可以接受用户通过键盘输入的内容。
6+
7+ 通过Python能不能实现这个简单的功能呢?当然能,要不然Python如何横行天下呀。
8+
9+ 不过在写这个功能前,要了解函数:
10+
11+ - Python 2:` raw_input() `
12+ - Python 3: ` input() `
13+
14+ 这是Python的内建函数(built-in function)。关于内建函数,可以分别通过下面的链接查看:
15+
16+ - [ Python 2的内建函数] ( https://docs.python.org/2/library/functions.html )
17+ - [ Python 3的内建函数] ( https://docs.python.org/3.5/library/functions.html )
18+
19+ 如果仔细对照上面的两个版本的内建函数,会发现还是有差异的。
4920
5021这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?
5122
52- 不知道你是否还记得我在前面使用过的方法,这里再进行演示,这种方法是学习python的法宝。
23+ 一种方法是通过网页上的官方内容,点击链接,就能查看该函数的说明文档。
24+
25+ 还有一种方法,不知道你是否还记得我在前面使用过的,这里再进行演示。
5326
54- >>> help(raw_input)
27+ >>> help(raw_input) #Python 2
5528
56- 然后就出现:
29+ >>> help(input) #Python 3
30+
31+ 然后就出现,
32+
33+ Python 2中的结果:
5734
5835 Help on built-in function raw_input in module __builtin__:
5936
@@ -65,128 +42,152 @@ Built-in Functions
6542 On Unix, GNU readline is used if enabled. The prompt string, if given,
6643 is printed without a trailing newline before reading.
6744
68- 从中是不是已经清晰地看到了` raw_input() ` 的使用方法了。
69-
70- 还有第二种方法,那就是到python的官方网站,查看内建函数的说明。https://docs.python.org/2/library/functions.html
45+ Python 3中的结果:
7146
72- 其实,我上面那个表格,就是在这个网页中抄过来的。
47+ Help on built-in function input in module builtins:
7348
74- 例如,对` print() ` 说明如下:
75-
76- print(*objects, sep=' ', end='\n', file=sys.stdout)
77-
78- Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
79-
80- All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
49+ input(prompt=None, /)
50+ Read a string from standard input. The trailing newline is stripped.
51+
52+ The prompt string, if given, is printed to standard output without a
53+ trailing newline before reading input.
54+
55+ If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
56+ On *nix systems, readline is used if available.
57+
58+ 从中是不是已经清晰地看到了` raw_input() ` 或者` input() ` 的使用方法了。
8159
82- The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.
60+ 下面就在交互模式下操练一下这个主管键盘输入的函数。
8361
8462分别在交互模式下,将这个两个函数操练一下。
8563
86- >>> raw_input("input your name:")
87- input your name:python
64+ >>> raw_input("input your name:") # Python 2
65+ input your name:python #提示输入内容,通过键盘输入`python`
8866 'python'
89-
90- 输入名字之后,就返回了输入的内容。用一个变量可以获得这个返回值。
67+
68+ >>> input("input your name:") #Python 3
69+ input your name:python #提示输入内容,通过键盘输入`python`
70+ 'python'
71+
72+ 输入名字之后,就返回了输入的内容。
9173
92- >>> name = raw_input("input your name:")
74+ 返回的结果,也是一个对象(字符串类型的对象),那么就可以用赋值语句,与一个变量关联起来。
75+
76+ >>> name = raw_input("input your name:") #Python 2
9377 input your name:python
9478 >>> name
9579 'python'
9680 >>> type(name)
9781 <type 'str'>
98-
82+
83+ >>> name = input("input your name:") #Python 3
84+ input your name:python
85+ >>> name
86+ 'python'
87+ >>> type(name)
88+ <class 'str'>
89+
9990而且,返回的结果是str类型。如果输入的是数字呢?
10091
101- >>> age = raw_input("How old are you?")
92+ >>> age = raw_input("How old are you?") #Python 2
10293 How old are you?10
10394 >>> age
10495 '10'
10596 >>> type(age)
10697 <type 'str'>
10798
108- 返回的结果,仍然是str类型。
99+ >>> age = input("How old are you?") #Python 2
100+ How old are you?10
101+ >>> age
102+ '10'
103+ >>> type(age)
104+ <class 'str'>
109105
110- 再试试 ` print() ` ,看前面对它的说明,是比较复杂的。没关系,我们从简单的开始。在交互模式下操作:
106+ 返回的结果,仍然是str类型。
111107
112- >>> print("hello, world")
113- hello, world
114- >>> a = "python"
115- >>> b = "good"
116- >>> print a
117- python
118- >>> print a,b
119- python good
108+ 所以,Python 2的文档中就明确写出` raw_input([prompt]) -> string ` ,意思是它的返回值为字符串。
120109
121- 比较简单吧。当然,这是没有搞太复杂了 。
110+ ` print() ` 在Python 2和Python 3中,都是一个函数 。
122111
123- 特别要提醒的是,` print() ` 默认是以` \n ` 结尾的,所以,会看到每个输出语句之后 ,输出内容后面自动带上了` \n ` ,于是就换行了 。
112+ 特别要提醒的是,` print() ` 默认是以` \n ` 结尾的,所以,每次用到 ` print ` 或者 ` print() ` 之后 ,输出内容后面自动带上了` \n ` ,于是在打印的结果中就换行了 。
124113
125114有了以上两个准备,接下来就可以写一个能够“对话”的小程序了。
126115
127116 #!/usr/bin/env python
128117 # coding=utf-8
129118
130- name = raw_input("What is your name?")
119+ name = raw_input("What is your name?") #如果是在Python 3中,更换为input()
131120 age = raw_input("How old are you?")
132121
133- print "Your name is:", name
134- print "You are " + age + " years old."
122+ print "Your name is: ", name #Python 3: print("Your name is: ", name)
123+ print "You are " + age + " years old." #Python 3: print("You are " + age + " years old.")
135124
136125 after_ten = int(age) + 10
137- print "You will be " + str(after_ten) + " years old after ten years."
126+ print "You will be " + str(after_ten) + " years old after ten years."
127+ #Python 3: print("You will be " + str(after_ten) + " years old after ten years.")
138128
139- 对这段小程序中,有几点说明
129+ 读者是否能独立调试这个程序?
140130
141- 前面演示了 ` print() ` 的使用 ,除了打印一个字符串之外,还可以打印字符串拼接结果。
131+ ` print ` 语句或者 ` print ()` 函数 ,除了打印一个字符串之外,还可以打印字符串拼接结果(拼接之后还是一个字符串,就是比原来长了) 。
142132
143- print "You are " + age + " years old."
133+ print "You are " + age + " years old." #Python 2
134+ print("You are " + age + " years old.") #Python 3
144135
145- 注意,那个变量` age ` 必须是字符串 ,如最后的那个语句中:
136+ 注意,那个变量` age ` 必须指向的是字符串类型的对象 ,如最后的那个语句中:
146137
147- print "You will be " + str(after_ten) + " years old after ten years."
148-
149- 这句话里面,有一个类型转化,将原本是整数型` after_ten ` 转化为了str类型。否则,就包括,不信,你可以试试。
138+ print "You will be " + str(after_ten) + " years old after ten years." #Python 2
139+ print("You will be " + str(after_ten) + " years old after ten years.") #Python 3
140+
141+ 这句话里面,有一个类型转化,将原本是整数型的对象转化为了str类型。否则,就报错,不信,你可以试试。
142+
143+ 同样注意,在` after_ten = int(age) + 10 ` 中,因为通过` raw_input() ` 或者` input() ` 得到的是str类型,当age和10求和的时候,需要先用` int() ` 函数进行类型转化,才能和后面的整数10相加。
150144
151- 同样注意,在 ` after_ten = int(age) + 10 ` 中,因为通过 ` raw_input ` 得到的是str类型,当age和10求和的时候,需要先用 ` int() ` 函数进行类型转化,才能和后面的整数10相加 。
145+ 这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请仔细调试一下,如果没有通过,看报错信息,你能够从中获得修改方向的信息 。
152146
153- 这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请看官调试一下,如果没有通过,仔细看报错信息,你能够从中获得修改方向的信息 。
147+ 通过键盘输入得到的都是字符串,也有的字符串不是通过键盘输入得到的,需要用引号包裹,有时候还要用转义符。但是,有一种方式,能够还原字符串中字符的原始含义 。
154148
155149##原始字符串
156150
157- 所谓原始字符串,就是指字符串里面的每个字符都是原始含义,比如反斜杠,不会被看做转义符。如果在一般字符串中,比如
151+ 所谓原始字符串,就是指字符串里面的每个字符都是原始含义,比如反斜杠,不会被看做转义符。
152+
153+ 在一般字符串中,比如
158154
159- >>> print "I like \npython"
155+ >>> print "I like \npython" #Python 3: print("I like \npython")
160156 I like
161157 python
162158
163- 这里的反斜杠就不是“反斜杠”的原始符号含义,而是和后面的n一起表示换行(转义了) 。当然,这似乎没有什么太大影响,但有的时候,可能会出现问题,比如打印DOS路径(DOS,有没有搞错,现在还有人用吗?)
159+ 这里的反斜杠就不是“反斜杠”的原始符号含义,而是和后面的n一起组成了换行符 ` \n ` ,即转义了 。当然,这似乎没有什么太大影响,但有的时候,可能会出现问题,比如打印DOS路径(DOS,有没有搞错,现在还有人用吗?)
164160
165161 >>> dos = "c:\news"
166162 >>> dos
167- 'c:\news' #这里貌似没有什么问题
163+ 'c:\news' #这里貌似没有什么问题
168164 >>> print dos #当用print来打印这个字符串的时候,就出问题了。
169165 c:
170166 ews
167+ #Python 3: print(dos)
168+
169+ 如何避免?
171170
172- 如何避免?用前面讲过的转义符可以解决 :
171+ 用转义符可以解决 :
173172
174173 >>> dos = "c:\\news"
175- >>> print dos
174+ >>> print dos #Python 3: print(dos)
176175 c:\news
177176
178177此外,还有一种方法,如:
179178
180179 >>> dos = r"c:\news"
181- >>> print dos
180+ >>> print dos #Python 3: print(dos)
182181 c:\news
183- >>> print r"c:\news\python"
182+ >>> print r"c:\news\python" #Python 3: print(r"c:\news\python")
184183 c:\news\python
185184
186185状如` r"c:\news" ` ,由r开头引起的字符串,就是原始字符串,在里面放任何字符都表示该字符的原始含义。
187186
188187这种方法在做网站设置网站目录结构的时候非常有用。使用了原始字符串,就不需要转义了。
189188
189+ 一个字符串,一般可以有多个字符构成,那么可以操作每个字符吗?这就要索引和切片。
190+
190191------
191192
192193[ 总目录] ( ./index.md )   ;  ;  ; |  ;  ;  ; [ 上节:字符串(1)] ( ./106.md )   ;  ;  ; |  ;  ;  ; [ 下节:字符串(3)] ( ./108.md )
0 commit comments