python的字符串有很多好用的操作,比如find,startswith命令。

这几个命令在处理配置文件的时候很有用,比如用startswith判断是否是注释行。

注意:几个函数的返回值是不同滴。

函数原型:find(str, pos_start, pos_end),返回-1或第一个查找到的位置。

              startswith(str),返回false和true

              endswith(str),返回false和true

find命令:

>>> str1 = 'hello, world'
>>> print str1.find('he')
0
>>> print str1.find('wo ')
-1

startswith命令和endswith命令:

>>> str1 = 'hello, world'
>>> print str1.startswith('he')
True
>>> print str1.endswith('ld')
True

 

相关文章:

  • 2021-12-05
猜你喜欢
  • 2021-06-05
  • 2022-02-12
  • 2021-08-11
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案