一、字符串类型

  • 按索引取值
>>> str = 'hello word!'
>>> print (str[6])
w
>>> print (str[0])
h
>>> print (str[10])
!
  • 切片
>>> print (str[0:6])
hello 
>>> print (str[0:9])
hello wor
>>> print (str[4:9])
o wor
  • 长度len
>>> print (len(str))
11
>>> 
  • 成员运算in | not in
>>> print ('he' in str)
True
>>> print ('he' not in str)
False
>>> print ('ho' not in str)
True
>>> print ('ho'  in str)
False
>>> print ('hh'  in str)
False
  • 移除strip

strip移除字符串头尾指定的字符序列(移除空格)

>>> str = 'hello word!'
>>> print(str.strip())
hello word!
>>> text = '  pig  '
>>> print(text.strip())
pig
>>> text = '  pig'
>>> print(text.strip())
pig
>>> text = '***pig*****'
>>> print(text.strip('*'))
pig
>>> 
  • 切分split
>>> print(text.split(','))
['aaa', 'bbb:cdcd!']
>>> 
最后修改:2020 年 11 月 13 日 06 : 09 PM
如果觉得我的文章对你有用,请随意赞赏