python操作几种字符串

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

  >>> ‘spam eggs’

  ’spam eggs’

  >>> ‘doesn\’t’ “doesn’t”

  >>> “doesn’t” “doesn’t”

  >>> ‘”Yes,” he said.’

  ’”Yes,” he said.’

  >>> “\”Yes,\” he said.”

  ’”Yes,” he said.’

  >>> ‘”Isn\’t,” she said.’

  ’”Isn\’t,” she said.’

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

  hello = “This is a rather long string containing\n\

  several lines of text just as you would do in C.\n\

  Note that whitespace at the beginning of the line is\

  significant.”

  print hello

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

  This is a rather long string containing

  several lines of text just as you would do in C.

  Note that whitespace at the beginning of the line is significant.

  然而,如果我们创建一个“raw”行,\n序列就不会转为换行,示例源码最后的反斜杠和换行符n都会做为字符 串中的数据处理。如下所示:

  hello = r”This is a rather long string containing\n\

  several lines of text much as you would do in C.”

  print hello

  会打印为:

  This is a rather long string containing\n\

  several lines of text much as you would do in C.

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

  print “”"

  Usage: thingy [OPTIONS]

  -h Display this usage message

  -H hostname Hostname to connect to

  ”"”

  produces the following output:

  Usage: thingy [OPTIONS]

  -h Display this usage message

  -H hostname Hostname to connect to

  解释器打印出来的字符串与它们输入的形式完全相同:内部的引号,用反斜杠标识的引号和各种怪字符,都精确的显示出来 。如果字符串中包含单引号,不包含双引号,可以用双引号引用它,反之可以用单引号。(后面 介绍的print语句,可以可以用来写没有引号和反斜杠的字符串)。

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

  >>> word = ‘Help’ + ‘A’

  >>> word

  ’HelpA’

  >>> ‘<’ + word*5 + ‘>’

  ”

  两个字符串值之间的联接是自动的,上例第一行可以写成“word = ‘Help’ ‘A’”这种方式只对字符串值有效,任 何字符串表达式都不适用这种方法。

  >>> import string

  >>> ‘str’ ‘ing’ #<-This is ok

  ’string’

  >>> string.strip(‘str’)+ ‘ing’#<-This is ok

  ’string’

  >>> string.strip(‘str’)'ing’#<-This is invalid

  File “”, line 1, in ?

  string.strip(‘str’) ‘ing’

  ^

  SyntaxError: invalid syntax

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

  >>> word[4]

  ’A’

  >>> word[0:2]

  ’He’

  >>> word[2:4]

  ’lp’

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

  >>> word[:2] # The first two characters

  ’He’

  >>> word[2:] # All but the first two characters

  ’lpA’

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

  >>> word[0] = ‘x’

  Traceback (most recent call last): File “”, line 1, in ?

  TypeError: object doesn’t support item assignment

  >>> word[:1] = ‘Splat’

  Traceback (most recent call last): File “”, line 1, in ?

  TypeError: object doesn’t support slice assignment

  然而,可以通过简单有效的组合方式生成新的字符串:

  >>> ‘x’ + word[1:]

  ’xelpA’

  >>> ‘Splat’ + word[4]

  ’SplatA’

  切片操作有一个很有用的不变性: s[:i] + s[i:] 等于s。

  >>> word[:2] + word[2:]

  ’HelpA’

  >>> word[:3] + word[3:]

  ’HelpA’

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

  >>> word[1:100]

  ’elpA’

  >>> word[10:]

  ”

  >>> word[2:1]

  ”

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

  >>> word[-1] # The last character

  ’A’

  >>> word[-2] # The last-but-one character

  ’p’

  >>> word[-2:] # The last two characters

  ’pA’

  >>> word[:-2] # All but the last two characters

  ’Hel’

  不过-0还是0,所以它不是从右边计数的!

  >>> word[-0] # (since -0 equals 0)

  ’H’

  越界的负切片索引会被截断,不过不要尝试在前元素索引(非切片的)中这样做:

  >>> word[-100:]

  ’HelpA’

  >>> word[-10] # error

  Traceback (most recent call last): File “”, line 1, in ?

  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到5各个索引的位置,第二行是对应的负索引。从i到j的切片由这两个标志之间的字 符组成。

  对于非负索引,切片长度就是两


文章转自:www.jeapedu.com/blog/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值