python中str函数_Python:Dir及str函数

使用python写脚本时,使用到字符串的使用方法,和Java略有不同,还要翻Python文档。尽管Python文档很赞,但是总不如罗列方法,放入本文,以备随时查看。还有另外一点,如果能够随时得到对象的属性和方法,那最好不过啦,于是就有了本文。

Python有一部分内置函数相当有用,这部分内置函数被集中起来,其它函数被分到了各个模块中,这种设计非常高明,避免了核心语言像其它的脚本语言一样臃肿不堪。

内置参数有很多,本文主要讲解这两个,type、dir

type函数返回任意对象的数据类型,对于处理多数据类型的help函数非常管用。

>>> type(1)

>>> li = []

>>> type(li)

>>> li = {}

>>> type(li)

>>> type(type(li))

type可以接收任何东西作为参数――我的意思是任何东西――并返回它的数据类型。整型、字符串、列表、字典、元组、函数、类、模块,甚至类型对象都可以作为参数被type函数接受。

type可以接收变量作为参数,并返回它的数据类型。

type还可以作用于模块。

dir为python内建函数,能够返回任意对象的属性和方法列表,相当多的东西,这个函数是本文的重点介绍的函数。

>>> li = ''

>>> dir(li)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>> li = ()

>>> dir(li)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__str__']

>>> li = []

>>> dir(li)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>> li = {}

>>> dir(li)

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

看完这几个dir例子,应该了解的差不多了吧。

下面列下str的各个方法使用的简要说明,作为以后使用Python的备忘。

str.capitalize():首字母大写

str.center(width[, fillchar]):居中,多余的空白用fillchar填充,默认空格

str.count(sub[, start[, end]]):查询sub在出现的次数,能够配置start和end

str.decode([encoding[, errors]]):解码

str.encode([encoding[, errors]]):编码

str.endswith(suffix[, start[, end]]):

str.expandtabs([tabsize]):Tab替换为Space

str.find(sub[, start[, end]]):查找,可以设置start和end

str.format(*args, **kwargs):格式化规范比较多,可以细看文档

str.index(sub[, start[, end]]):查找

str.isalnum():

str.isalpha():

str.isdigit():

str.islower():

str.isspace():

str.istitle():

str.isupper():

str.join(iterable):连接,这个功能好用

str.ljust(width[, fillchar]):左对齐

str.lower()

str.lstrip([chars]):默认去除左边的空格,还可以去除左边的chars

str.partition(sep):V2.5以上可用

str.replace(old, new[, count])

str.rfind(sub[, start[, end]]):从右侧开始查找

str.rindex(sub[, start[, end]]):从右边开始查找索引

str.rjust(width[, fillchar]):右对齐

str.rpartition(sep):

str.rsplit([sep[, maxsplit]])

str.rstrip([chars])

str.split([sep[, maxsplit]]):拆分

str.splitlines([keepends])这个拆分支持CR、LF、CRLF

str.startswith(prefix[, start[, end]]):以prefix开头

str.strip([chars]):默认去除空格,还可以去除两边的chars

str.swapcase():大小写反转

str.title():单词首字母大写,注意是单词

str.translate(table[, deletechars]):这个算是映射吧,可以这样理解

str.upper()

str.zfill(width):width长度内左边补零

Tuple:这个变量的声明就是为了速度,没有提供扩展和修改方法。

List:提供了常用的insert、pop、remove、append等方法

Dict:提供了常用的pop、copy、clear、iter等方法。

总之一句话,如果有不懂的python方法或者对象,dir一下总是没错的。

大家可以执行下这句话看下:

>>> import __builtin__

>>> dir(__builtin__)

在Python中万物皆为对象,这种查看内存中以对象形式存在的模块和函数的功能被称为是自省,用这种自省方法,你可以定义没有名字的函数;不按函数声明的参数顺序调用;甚至引用你事先不知道名称的函数;这种自省的方法在Python中使用起来更加方便、顺手,大家平时在工作中办点比较琐碎的事情,那Python就是首选了。

分享到:

sina.jpg

tec.jpg

2012-11-22 20:05

浏览 2341

分类:互联网

评论

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值