Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3654
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-20 09:45
个人简介

Everything will change. The only question is growing up or decaying.

文章分类

全部博文(2)

文章存档

2014年(2)

我的朋友

分类: Python/Ruby

2014-05-20 16:32:07


在交互模式下,最近一次表达式输出 保存在_变量中。这意味着把Python当做桌面计算器使用时,可以 方便的进行连续计算,如:



  1. >>> tax = 12.5 / 100
  2. >>> price = 100.50
  3. >>> price * tax
  4. 12.5625
  5. >>> price + _
  6. 113.0625
  7. >>> round(_,2)
  8. 113.06


这个 “_”变量对于用户来说是只读的。不要试图去给它赋值 ,聊限于Python的语法规则,你只会创建一个同名的局部变量覆盖它。



字符串


除了数值,Python还可以通过几种不同的方法 操作字符串。字符串用单引号或双引号标识:



  1. >>> print 'spam eggs' #用单引号表示一个整体
  2. spam eggs
  3. >>> print 'doesn\'t' # \ 表示转义
  4. doesn't
  5. >>> print '"Yes," he said.' # 在单引号内,可允许字符输出引号
  6. "Yes," he said.
  7. >>> print "\"Yes,\" he said."
  8. "Yes," he said.
  9. >>> print '"Isn\t," she said.' # \t 相当于一个tab键。
  10. "Isn ," she said.
  11. >>> print '"Isn\'t," she said.'
  12. "Isn't,


字符串可以通过几种方式分行。可以在行加反斜杠做为继续符,这表示下一行是当前行的逻辑沿续。



  1. >>> hello = "This is a rather long string contraining\n\
  2. ... servral lines of text just as you would do in C.\n\
  3. ... Note that whitespace at the beginning of the line is\
  4. ... significant."
  5. >>> print hello
  6. This is a rather long string contraining
  7. servral lines of text just as you would do in C.
  8.  Note that whitespace at the beginning of the line is significant.


注意换行用\n来表示;反斜杠后面的新行标识(newline,缩写“n”)会转换为挑选符,示例会按原格式打印。


然而,如果我们创建一个“行”(“raw”)字符串,\ n序列就不会转为换行,源码中的反斜杠的换行符n都会被做为字符串中的数据外理。如:




  1. >>> hello = r"This is a rather long string containing\n\
  2. ... serveral lines of text much as you would do in C."
  3. >>> print hello
  4. This is a rather long string containing\n\
  5. serveral lines of text much as you would do in C.


另外,字符串可以用一对三重引号”“”或’‘’来标识。三重引号中的字符串在行尾不需要换行标记,所有的格式都会包括在字符串中。



  1. >>> print '''
  2. ... usage: thingy [OPTHIONS]
  3. ... -h Display this usage message
  4. ... -H hostname HostName to connect to
  5. ... '''

  6. usage: thingy [OPTHIONS]
  7.  -h Display this usage message
  8.  -H hostname HostName to connect to


打印出来的字符串与它们输入的形式完全相同:内部的引号,用反斜杠樯的引号和各种怪字符,都精确的显示出来 。如果字符串中包含单引号,不包含双引号,可以用双引号引用它,反之则使用单引号。




  1. >>> print " "hello" "
  2.   File "", line 1
  3.     print " "hello" "
  4.                  ^
  5. SyntaxError: invalid syntax
  6. >>> print ' "hello" ' #这是正确的写法,输出的内容是带双引号的,所以用单引号引用。
  7.  "hello"


   我发现,如果输出的字符串包含单引号或双引号时,直接使用单引号引用即可。



字符串可以用+号联接(也可以说粘合),也可以用*号循环。

上面这个例子也可以写成“ word= ‘help’’A’ ”这样子。这种方法只能字符串有效,任何字符串表达式都不适用这种方法。



  1. >>> print 'str' 'ing'
  2. string
  3. >>> print 'str'.strip() 'ing'
  4.   File "", line 1
  5.     print 'str'.strip() 'ing'
  6.                             ^
  7. SyntaxError: invalid syntax


字符串可以用下标(索引)查询;就像C一样,字符串的第一个字符下标是0。这里没有独立的字符类型,字符仅仅是大小为一的字符串。字符串的子串可以通过切片标示来表示:两个由冒号隔开的索引。



  1. >>> word[4]
  2. 'A'
  3. >>> word[1:4]
  4. 'elp'
  5. >>> word[0]
  6. 'h'
  7. >>> word[0:3]
  8. 'hel'


切片索引可以使用默认值;前一个索引默认值为0,后一个索引默认值为被切片的字符串的长度。



  1. >>> word[:3]
  2. 'hel'
  3. >>> word[3:]
  4. 'pA'


与C字符串不同,Python字符串不能改写。按字符串索引赋值会产生错误。



  1. >>> word[0] = 'x'
  2. Traceback (most recent call last):
  3.   File "", line 1, in <module>
  4. TypeError: 'str' object does not support item assignment
  5. >>> word[:1] = 'Splat'
  6. Traceback (most recent call last):
  7.   File "", line 1, in <module>
  8. TypeError: 'str' object does not support item assignment


不过确是可以通过简单有效的组合方式生成新的字符串:



  1. >>> word[:2] + 'XX'
  2. 'heXX'
  3. >>> 'AA' + word[3:]
  4. 'AApA'

切片操作有一个很有用的来变性:



  1. >>> word[:2] + word[2:]
  2. 'helpA'
  3. >>> word[:4] + word[4:]
  4. 'helpA'



切片索引处理方式很优美:过大的索引代替为字符串大小,下界比上界大时返回空字符串。



  1. >>> word[1:50]
  2. 'elpA'
  3. >>> word[10:]
  4. ''
  5. >>> word[2:1]
  6. ''


索引可以是负数,则从右边开始,如:



  1. >>> word[-1]
  2. 'A'
  3. >>> word[-2]
  4. 'p'
  5. >>> word[:-2]
  6. 'hel'


不过要注意的是,不管是-0还是0,它没有从右边计数,也就是说无论是-0还是0都是左边开头第一个字符串。



  1. >>> word[0]
  2. 'h'
  3. >>> word[-0]
  4. 'h'


但是,越界的负切片索引会被截断。



  1. >>> word[-100:]
  2. 'helpA'
  3. >>> word[-10]
  4. Traceback (most recent call last):
  5.   File "", line 1, in <module>
  6. 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() 返回字符串长度:


  1. >>> s = 'supercalifragilisticexpialidocious'
  2. >>> len(s)
  3. 34
  4. >>> b = '1231231231231231231231231231231231231'
  5. >>> len (b)
  6. 37

阅读(158) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~