1.in 和 not in区别
-
[root@oracle11g home]# ipython
-
/usr/lib/python2.6/site-packages/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated
-
from sets import Set
-
Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)
-
Type "copyright", "credits" or "license" for more information.
-
-
IPython 0.9.1 -- An enhanced Interactive Python.
-
? -> Introduction and overview of IPython's features.
-
%quickref -> Quick reference.
-
help -> Python's own help system.
-
object? -> Details about 'object'. ?object also works, ?? prints more.
-
-
In [1]: import subprocess
-
-
In [2]: res = subprocess.Popen(['uname', '-sv'], stdout = subprocess.PIPE)
-
-
In [3]: uname = res.stdout.read().strip()
-
-
In [4]: uname
-
Out[4]: 'Linux #1 SMP Wed Jun 13 18:24:36 EDT 2012'
-
-
In [5]: 'Linux' in uname
-
Out[5]: True
-
-
In [6]: '123' in uname
-
Out[6]: False
-
-
In [7]: '123' not in uname
-
Out[7]: True
2.find()和index()
有时候仅仅需要知道一个字符串中是否有另外一个字符串,但有时还需要字符串的具体位置,使用find()和index()可以实现这一目的。
-
In [8]: uname.index('Linux')
-
Out[8]: 0
-
-
In [9]: uname.index('Linux')
-
Out[9]: 0
-
-
In [10]: uname.find('Linux')
-
Out[10]: 0
-
-
In [11]: uname.index('Darwin')
-
---------------------------------------------------------------------------
-
ValueError Traceback (most recent call last)
-
-
/home/<ipython console> in <module>()
-
-
ValueError: substring not found
-
-
In [12]: uname.find('Darwin')
-
Out[12]: -1
3.切割
-
In [13]: smp_index=uname.index('SMP')
-
-
In [14]: smp_index
-
Out[14]: 9
-
-
In [15]: uname[smp_index:]
-
Out[15]: 'SMP Wed Jun 13 18:24:36 EDT 2012'
-
-
In [16]: uname[:smp_index]
-
Out[16]: 'Linux #1 '
-
-
In [17]: uname
-
Out[17]: 'Linux #1 SMP Wed Jun 13 18:24:36 EDT 2012'
4.startwith()和endwith()
-
In [18]: some_string = "Raymond Luxury-Yacht"
-
-
In [20]: some_string.startswith("Raymond")
-
Out[20]: True
-
-
In [21]: some_string.endswith("Throatwarbler")
-
Out[21]: False
-
-
In [22]: some_string.endswith("Luxury-Yacht")
-
Out[22]: True
-
-
In [23]: some_string.endswith("Mangrove")
-
Out[23]: False
阅读(1141) | 评论(0) | 转发(0) |