首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python常用内置函数

一些常用的内置函数

所谓内置函数,就是不用任何导入,语言本身就支持的函数:

print --- 打印输出

print var1, var2, var3

[python]view plain copy

[0, 1, 2, 3]

at 0x7f668c015140>

>>> print a, d

[0, 1, 2, 3] at 0x7f668c015140>

print与%结合更为强大:

print formats % (var1, var2, ...):

[python]view plain copy

>>> print "today is %d, welcome %s" % (2013, 'alex');

today is 2013, welcome alex

其实这没什么神秘的,前面提到过%格式化返回是一个字串,所以print仅是输出字串而已,格式化工作是由%来做的.

len()---返回列表,字串的长度

range([start], stop, [step]) --- 生成一个整数列表,从,start开始,到stop结束,以step为步长

[python]view plain copy

>>> range(4)

[0, 1, 2, 3]

>>> range(1,4)

[1, 2, 3]

>>> range(1,4,2)

[1, 3]

help(func)---获取某个函数的帮助文档.

执行系统命令行命令

import subprocess;

check_call(commands, shell=True)可以执行一个命令,并检查结果:

[python]view plain copy

>>> check_call("ls -l .", shell=True);

total 380

-rw-r--r-- 1 alex alex 303137 Jun 28 23:25 00005.vcf

-rw-rw-r-- 1 alex alex 1127 Jun 28 23:45 contacts.txt

-rw-rw-r-- 1 alex alex 3349 Jun 29 00:19 contacts_vcard.vcf

-rw-r--r-- 1 alex alex 8445 Jun 15 18:17 examples.desktop

-rw-rw-r-- 1 alex alex 0 Jun 19 20:21 libpeerconnection.log

-rw-rw-r-- 1 alex alex 148 Jul 4 22:46 persons.txt

-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py

-rw-rw-r-- 1 alex alex 271 Jul 4 21:28 speech.txt

check_call是相当于在Shell上执行一个语句,所以可以发挥想像力,组合Shell命令:

[python]view plain copy

>>> check_call("ls -l . | grep 'py'", shell=True);

-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py

所以,这是相当强大的工具,可以像写Shell脚本那样,结合管道干一些大事!

check_output(cmds, shell=True)执行命令,并以字串形式返回结果:

[python]view plain copy

>>> a = check_output("ls -l .", shell=True);

'total 380\n-rw-r--r-- 1 alex alex 303137 Jun 28 23:25 00005.vcf\ndrwxrwxr-x 3 alex alex 4096 Jun 28 23:57 3730996syntheticseismogram\n-rw-rw-r-- 1 alex alex 1127 Jun 28 23:45 contacts.txt\n-rw-rw-r-- 1 alex alex 3349 Jun 29 00:19 contacts_vcard.vcf\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Desktop\ndrwxr-xr-x 3 alex alex 4096 Jun 22 08:59 Documents\ndrwxr-xr-x 9 alex alex 4096 Jul 3 20:34 Downloads\n-rw-r--r-- 1 alex alex 8445 Jun 15 18:17 examples.desktop\ndrwxrwxr-x 5 alex alex 4096 Jun 19 23:01 gitting\n-rw-rw-r-- 1 alex alex 0 Jun 19 20:21 libpeerconnection.log\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Music\n-rw-rw-r-- 1 alex alex 148 Jul 4 22:46 persons.txt\ndrwxr-xr-x 3 alex alex 4096 Jul 4 23:08 Pictures\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Public\n-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py\n-rw-rw-r-- 1 alex alex 271 Jul 4 21:28 speech.txt\n-rw-rw-r-- 1 alex alex 93 Jul 3 23:02 speech.txt.bak\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Templates\ndrwxrwxr-x 2 alex alex 4096 Jun 22 19:01 Ubuntu One\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Videos\n'

>>> b = check_output("ls -l . | grep 'py'", shell=True);

'-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py\n'

不用我说你就知道它的强大之处了!唯一需要注意的就是换行符也在里面,处理的时候需要注意!

正则表达式

Python也是支持正则表达式的,至于正则表达式,跟其他的语言如Java,C没什么差别,这里说说如何使用正则表达式来进行匹配:

[python]view plain copy

import re;

p = re.compile(expression);

m = p.search(target);

if m != None:

# got match

else:

# no match

如:

[python]view plain copy

>>> message = 'Welcome to the year of 2013';

>>> import re;

>>> p = re.compile('(\d+)');

>>> m = p.search(message);

>>> print m.group(1)

2013

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180116A0UZ5L00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券