Skip to content

Commit d2c4918

Browse files
committed
string 2
1 parent b8a76d2 commit d2c4918

3 files changed

Lines changed: 157 additions & 1 deletion

File tree

107.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#字符串(2)
2+
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+
|all() | enumerate() | int() | ord() | str()|
19+
|any() | eval() | isinstance()| pow()| sum()|
20+
|basestring() | execfile() | issubclass() | print() | super()|
21+
|bin() | file() | iter()| property()| tuple()|
22+
|bool() | filter() | len() | range() | type()|
23+
|bytearray() | float()| list() | raw_input()| unichr()|
24+
|callable() | format() | locals() | reduce() | unicode()|
25+
|chr() | frozenset() | long() | reload() | vars()|
26+
|classmethod()| getattr()| map() | repr() | xrange()|
27+
|cmp() | globals()| max()| reversed()| zip()|
28+
|compile() |hasattr() | memoryview()| round() | __import__()|
29+
|complex() |hash() | min()| set() | apply()|
30+
|delattr() |help()| next()| setattr()| buffer()|
31+
|dict() | hex() |object() |slice() | coerce()|
32+
|dir() | id() |oct() |sorted() |intern()|
33+
34+
这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?
35+
36+
不知道你是否还记得我在前面使用过的方法,这里再进行演示,这种方法是学习python的法宝。
37+
38+
>>> help(raw_input)
39+
40+
然后就出现:
41+
42+
Help on built-in function raw_input in module __builtin__:
43+
44+
raw_input(...)
45+
raw_input([prompt]) -> string
46+
47+
Read a string from standard input. The trailing newline is stripped.
48+
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
49+
On Unix, GNU readline is used if enabled. The prompt string, if given,
50+
is printed without a trailing newline before reading.
51+
52+
从中是不是已经清晰地看到了`raw_input()`的使用方法了。
53+
54+
还有第二种方法,那就是到python的官方网站,查看内建函数的说明。https://docs.python.org/2/library/functions.html
55+
56+
其实,我上面那个表格,就是在这个网页中抄过来的。
57+
58+
例如,对`print()`说明如下:
59+
60+
print(*objects, sep=' ', end='\n', file=sys.stdout)
61+
62+
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.
63+
64+
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.
65+
66+
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.
67+
68+
分别在交互模式下,将这个两个函数操练一下。
69+
70+
>>> raw_input("input your name:")
71+
input your name:python
72+
'python'
73+
74+
输入名字之后,就返回了输入的内容。用一个变量可以获得这个返回值。
75+
76+
>>> name = raw_input("input your name:")
77+
input your name:python
78+
>>> name
79+
'python'
80+
>>> type(name)
81+
<type 'str'>
82+
83+
而且,返回的结果是str类型。如果输入的是数字呢?
84+
85+
>>> age = raw_input("How old are you?")
86+
How old are you?10
87+
>>> age
88+
'10'
89+
>>> type(age)
90+
<type 'str'>
91+
92+
返回的结果,仍然是str类型。
93+
94+
再试试`print()`,看前面对它的说明,是比较复杂的。没关系,我们从简单的开始。在交互模式下操作:
95+
96+
>>> print("hello, world")
97+
hello, world
98+
>>> a = "python"
99+
>>> b = "good"
100+
>>> print a
101+
python
102+
>>> print a,b
103+
python good
104+
105+
比较简单吧。当然,这是没有搞太复杂了。
106+
107+
特别要提醒的是,`print()`默认是以`\n`结尾的,所以,会看到每个输出语句之后,输出内容后面自动带上了`\n`,于是就换行了。
108+
109+
有了以上两个准备,接下来就可以写一个能够“对话”的小程序了。
110+
111+
#!/usr/bin/env python
112+
# coding=utf-8
113+
114+
name = raw_input("What is your name?")
115+
age = raw_input("How old are you?")
116+
117+
print "Your name is:", name
118+
print "You are " + age + " years old."
119+
120+
after_ten = int(age) + 10
121+
print "You will be " + str(after_ten) + " years old after ten years."
122+
123+
对这段小程序中,有几点说明
124+
125+
前面演示了`print()`的使用,除了打印一个字符串之外,还可以打印字符串拼接结果。
126+
127+
print "You are " + age + " years old."
128+
129+
注意,那个变量`age`必须是字符串,如最后的那个语句中:
130+
131+
print "You will be " + str(after_ten) + " years old after ten years."
132+
133+
这句话里面,有一个类型转化,将原本是整数型`after_ten`转化为了str类型。否则,就包括,不信,你可以试试。
134+
135+
同样注意,在`after_ten = int(age) + 10`中,因为通过`raw_input`得到的是str类型,当age和10求和的时候,需要先用`int()`函数进行类型转化,才能和后面的整数10相加。
136+
137+
这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请看官调试一下,如果没有通过,仔细看报错信息,你能够从中获得修改方向的信息。
138+
139+
------
140+
141+
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:字符串(1)](./106.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:字符串(3)](./108.md)
142+
143+
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。
144+

1code/107.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
name = raw_input("What is your name?")
5+
age = raw_input("How old are you?")
6+
7+
print "Your name is:", name
8+
print "You are " + age + " years old."
9+
10+
after_ten = int(age) + 10
11+
print "You will be " + str(after_ten) + " years old after ten years."

index.md

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

0 commit comments

Comments
 (0)