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

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

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

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

>>> type(1)
<type 'int'>
>>> li = []
>>> type(li)
<type 'list'>
>>> li = {}
>>> type(li)
<type 'dict'>
>>> type(type(li))
<type 'type'>

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__)

更多的内置函数看这里:http://docs.python.org/2/library/functions.html

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

Python:Dir及str函数相关推荐

  1. python中的str()函数

    1.python中的str()用来避免数据类型匹配错误 例如: 运行下列代码 num=19960223 info="my name is z,my birthday is" mes ...

  2. Python中的str()函数和repr()函数

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str() 或者 repr() . >>> a = 10 >>> type(str( ...

  3. Python基础 类型转换str()函数,int()函数与float()函数

    为什么需要数据类型转换         将不同数据类型的数据拼接在一起 当不同类型拼接在一起时,会产生错误 print('我叫'+name+',今年'+20+'岁了') 因此我们需要通过类型转换来解决 ...

  4. python dir用法_python函数之dir()函数

    dir()函数 中文说明: 你可以使用内建的dir函数来列出模块定义的标识符.标识符有函数.类和变量. 当你为dir()提供一个模块名的时候,它返回模块定义的名称列表.如果不提供参数,它返回当前模块中 ...

  5. python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

    python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...

  6. print函数python_带有结束参数的Python print()函数

    print函数python print()函数 (print() function) print() function is used to print message on the screen. ...

  7. dir函数python_Python dir()函数

    dir函数python Python dir() function attempts to return a list of valid attributes for the given object ...

  8. python中str是什么函数_Python str()函数

    描述 str函数是Python的内置函数,它将参数转换成字符串类型,即人适合阅读的形式. 语法 str(object) 名称说明备注 object待被转换成字符串的参数可省略的参数 返回值:返回obj ...

  9. python中str函数_一文让你彻底搞懂Python中__str__和__repr__?

    __str__和__repr__的异同? 字符串的表示形式 我们都知道,Python的内置函数repr()能够把对象用字符串的形式表达出来,方便我们辨认.这就是"字符串表示形式". ...

  10. Python str 函数 - Python零基础入门教程

    目录 一.Python str 函数介绍 二.Python str 函数使用 三.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 一 ...

最新文章

  1. 集成、知识蒸馏和自蒸馏有区别?
  2. swoole php配置文件,easyswoole自义命令加载自定义配置文件
  3. IJCAI 2021 | 中科院计算所:自监督增强的知识蒸馏方法
  4. boost::contract模块实现lambda表达式的测试程序
  5. SAP Spartacus Org unit detail实例的单例特性
  6. 出现在嵌入式DSP上可用于实现各种编解码器
  7. win10系统安装提示带有gui的服务器,安装win10提示“由于技术员系统中无接入音频设备,要启动GUI”如何...
  8. python获取gps_Python GPS模块:读取最新的GPS D
  9. Openwrt常用软件模块之CWMP
  10. 信息泄露事件频发 快递行业的隐私面单之战
  11. 基于C++实现的选课系统
  12. 计算机概论在线阅读,计算机科学概论(Python版)
  13. 前端可视化——Canvas
  14. arduino uno r3单片机封装图_Arduino教程 Lesson 1 驱动安装及下载Blink程序
  15. 卷积神经网络模型搭建(水果识别项目)
  16. 安装阿里基于Kaldi开源语音识别模型DFSMN
  17. Nginx真的消除了惊群效应么?不
  18. html 添加audio 无法自动播放,移动端不支持audio自动播放解决方案
  19. 只有极少数人能通过的「超级多任务」测试(文末附测试地址) 1
  20. vlookup匹配 匹配结果错误_明明有数据,为什么我的VLOOKUP总是匹配不出来?

热门文章

  1. Windows下PuTTY远程连接Linux服务器并上传文件/更改默认端口号
  2. 有哪些有关java类最新发表的毕业论文呢?
  3. 多线程学习-基础( 十)一个synchronized(){/*代码块*/}简单案例分析
  4. 计算机网络设置端口转发,怎么设置路由器端口转发功能?
  5. Archlinux 的灵魂──PKGBUILD、AUR 和 ABS
  6. ESP8266在局域网和互联网下的使用
  7. matplotlib色彩(colors)之图表数据系列默认配色(默认色彩循环)
  8. 抖音多闪数次声明被打脸 法院正式裁定抖音违规
  9. mysql pk什么意思_数据库pk什么意思
  10. 如何解决笔记本键盘突然失灵的问题