Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1061918
  • 博文数量: 239
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3618
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-12 13:17
文章分类

全部博文(239)

文章存档

2021年(1)

2016年(1)

2015年(30)

2014年(91)

2013年(116)

分类: LINUX

2014-02-22 22:06:25

1.in 和 not in区别

点击(此处)折叠或打开

  1. [root@oracle11g home]# ipython
  2. /usr/lib/python2.6/site-packages/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated
  3.   from sets import Set
  4. Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)
  5. Type "copyright", "credits" or "license" for more information.

  6. IPython 0.9.1 -- An enhanced Interactive Python.
  7. ? -> Introduction and overview of IPython's features.
  8. %quickref -> Quick reference.
  9. help -> Python's own help system.
  10. object? -> Details about 'object'. ?object also works, ?? prints more.

  11. In [1]: import subprocess

  12. In [2]: res = subprocess.Popen(['uname', '-sv'], stdout = subprocess.PIPE)

  13. In [3]: uname = res.stdout.read().strip()

  14. In [4]: uname
  15. Out[4]: 'Linux #1 SMP Wed Jun 13 18:24:36 EDT 2012'

  16. In [5]: 'Linux' in uname
  17. Out[5]: True

  18. In [6]: '123' in uname
  19. Out[6]: False

  20. In [7]: '123' not in uname
  21. Out[7]: True

2.find()和index()
有时候仅仅需要知道一个字符串中是否有另外一个字符串,但有时还需要字符串的具体位置,使用find()和index()可以实现这一目的。

点击(此处)折叠或打开

  1. In [8]: uname.index('Linux')
  2. Out[8]: 0

  3. In [9]: uname.index('Linux')
  4. Out[9]: 0

  5. In [10]: uname.find('Linux')
  6. Out[10]: 0

  7. In [11]: uname.index('Darwin')
  8. ---------------------------------------------------------------------------
  9. ValueError Traceback (most recent call last)

  10. /home/<ipython console> in <module>()

  11. ValueError: substring not found

  12. In [12]: uname.find('Darwin')
  13. Out[12]: -1
3.切割

点击(此处)折叠或打开

  1. In [13]: smp_index=uname.index('SMP')

  2. In [14]: smp_index
  3. Out[14]: 9

  4. In [15]: uname[smp_index:]
  5. Out[15]: 'SMP Wed Jun 13 18:24:36 EDT 2012'

  6. In [16]: uname[:smp_index]
  7. Out[16]: 'Linux #1 '

  8. In [17]: uname
  9. Out[17]: 'Linux #1 SMP Wed Jun 13 18:24:36 EDT 2012'
4.startwith()和endwith()

点击(此处)折叠或打开

  1. In [18]: some_string = "Raymond Luxury-Yacht"

  2. In [20]: some_string.startswith("Raymond")
  3. Out[20]: True

  4. In [21]: some_string.endswith("Throatwarbler")
  5. Out[21]: False

  6. In [22]: some_string.endswith("Luxury-Yacht")
  7. Out[22]: True

  8. In [23]: some_string.endswith("Mangrove")
  9. Out[23]: False

阅读(1141) | 评论(0) | 转发(0) |
0

上一篇:linux ipython安装

下一篇:python读取文本行数

给主人留下些什么吧!~~