阿牛 发表于 2017-4-25 09:32:54

Python 基础一(String 处理)

  Python 里面 String的处理:
  1.基本的‘和“处理,不用多说,直接上例子:

>>> '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.'
  2. String 对象可以被简单重复 

>>> word = 'Help' + 'A'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
  3.python的substring 比较好用。index是从0开始,像word

>>> word = 'Help' + 'A'
>>> word
'A'
>>> word
'He'
>>> word
'lp'
>>> word[:2]    # The first two characters
'He'
>>> word    # Everything except the first two characters
'lpA'
  但对String里面的substring直接赋值却不行。
页: [1]
查看完整版本: Python 基础一(String 处理)