在交互模式下,最近一次表达式输出 保存在_变量中。这意味着把Python当做桌面计算器使用时,可以 方便的进行连续计算,如:
-
>>> tax = 12.5 / 100
-
>>> price = 100.50
-
>>> price * tax
-
12.5625
-
>>> price + _
-
113.0625
-
>>> round(_,2)
-
113.06
这个 “_”变量对于用户来说是只读的。不要试图去给它赋值 ,聊限于Python的语法规则,你只会创建一个同名的局部变量覆盖它。
字符串
除了数值,Python还可以通过几种不同的方法 操作字符串。字符串用单引号或双引号标识:
-
>>> print 'spam eggs' #用单引号表示一个整体
-
spam eggs
-
>>> print 'doesn\'t' # \ 表示转义
-
doesn't
-
>>> print '"Yes," he said.' # 在单引号内,可允许字符输出引号
-
"Yes," he said.
-
>>> print "\"Yes,\" he said."
-
"Yes," he said.
-
>>> print '"Isn\t," she said.' # \t 相当于一个tab键。
-
"Isn ," she said.
-
>>> print '"Isn\'t," she said.'
-
"Isn't,
字符串可以通过几种方式分行。可以在行加反斜杠做为继续符,这表示下一行是当前行的逻辑沿续。
-
>>> hello = "This is a rather long string contraining\n\
-
... servral lines of text just as you would do in C.\n\
-
... Note that whitespace at the beginning of the line is\
-
... significant."
-
>>> print hello
-
This is a rather long string contraining
-
servral lines of text just as you would do in C.
-
Note that whitespace at the beginning of the line is significant.
注意换行用\n来表示;反斜杠后面的新行标识(newline,缩写“n”)会转换为挑选符,示例会按原格式打印。
然而,如果我们创建一个“行”(“raw”)字符串,\ n序列就不会转为换行,源码中的反斜杠的换行符n都会被做为字符串中的数据外理。如:
-
>>> hello = r"This is a rather long string containing\n\
-
... serveral lines of text much as you would do in C."
-
>>> print hello
-
This is a rather long string containing\n\
-
serveral lines of text much as you would do in C.
另外,字符串可以用一对三重引号”“”或’‘’来标识。三重引号中的字符串在行尾不需要换行标记,所有的格式都会包括在字符串中。
-
>>> print '''
-
... usage: thingy [OPTHIONS]
-
... -h Display this usage message
-
... -H hostname HostName to connect to
-
... '''
-
-
usage: thingy [OPTHIONS]
-
-h Display this usage message
-
-H hostname HostName to connect to
打印出来的字符串与它们输入的形式完全相同:内部的引号,用反斜杠樯的引号和各种怪字符,都精确的显示出来 。如果字符串中包含单引号,不包含双引号,可以用双引号引用它,反之则使用单引号。
-
>>> print " "hello" "
-
File "", line 1
-
print " "hello" "
-
^
-
SyntaxError: invalid syntax
-
>>> print ' "hello" ' #这是正确的写法,输出的内容是带双引号的,所以用单引号引用。
-
"hello"
我发现,如果输出的字符串包含单引号或双引号时,直接使用单引号引用即可。
字符串可以用+号联接(也可以说粘合),也可以用*号循环。
上面这个例子也可以写成“ word= ‘help’’A’ ”这样子。这种方法只能字符串有效,任何字符串表达式都不适用这种方法。
-
>>> print 'str' 'ing'
-
string
-
>>> print 'str'.strip() 'ing'
-
File "", line 1
-
print 'str'.strip() 'ing'
-
^
-
SyntaxError: invalid syntax
字符串可以用下标(索引)查询;就像C一样,字符串的第一个字符下标是0。这里没有独立的字符类型,字符仅仅是大小为一的字符串。字符串的子串可以通过切片标示来表示:两个由冒号隔开的索引。
-
>>> word[4]
-
'A'
-
>>> word[1:4]
-
'elp'
-
>>> word[0]
-
'h'
-
>>> word[0:3]
-
'hel'
切片索引可以使用默认值;前一个索引默认值为0,后一个索引默认值为被切片的字符串的长度。
-
>>> word[:3]
-
'hel'
-
>>> word[3:]
-
'pA'
与C字符串不同,Python字符串不能改写。按字符串索引赋值会产生错误。
-
>>> word[0] = 'x'
-
Traceback (most recent call last):
-
File "", line 1, in <module>
-
TypeError: 'str' object does not support item assignment
-
>>> word[:1] = 'Splat'
-
Traceback (most recent call last):
-
File "", line 1, in <module>
-
TypeError: 'str' object does not support item assignment
不过确是可以通过简单有效的组合方式生成新的字符串:
-
>>> word[:2] + 'XX'
-
'heXX'
-
>>> 'AA' + word[3:]
-
'AApA'
切片操作有一个很有用的来变性:
-
>>> word[:2] + word[2:]
-
'helpA'
-
>>> word[:4] + word[4:]
-
'helpA'
切片索引处理方式很优美:过大的索引代替为字符串大小,下界比上界大时返回空字符串。
-
>>> word[1:50]
-
'elpA'
-
>>> word[10:]
-
''
-
>>> word[2:1]
-
''
索引可以是负数,则从右边开始,如:
-
>>> word[-1]
-
'A'
-
>>> word[-2]
-
'p'
-
>>> word[:-2]
-
'hel'
不过要注意的是,不管是-0还是0,它没有从右边计数,也就是说无论是-0还是0都是左边开头第一个字符串。
-
>>> word[0]
-
'h'
-
>>> word[-0]
-
'h'
但是,越界的负切片索引会被截断。
-
>>> word[-100:]
-
'helpA'
-
>>> word[-10]
-
Traceback (most recent call last):
-
File "", line 1, in <module>
-
IndexError: string index out of range
理解切片的最好方式就是把索引视为两个字符之间的点,第一个字符的左边为0,字符串中第n个字符的右边是索引n,如:
+---+---+---+---+---+
| h | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1 -0
第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i到j的切片由这两个标志之间的字符组成。
对于非负索引,切片长度就是两索引的差。例如,word[1:3]的长度是2。
内置函数len() 返回字符串长度:
-
>>> s = 'supercalifragilisticexpialidocious'
-
>>> len(s)
-
34
-
>>> b = '1231231231231231231231231231231231231'
-
>>> len (b)
-
37
阅读(158) | 评论(0) | 转发(0) |