tip:

和tuple一样,字符串也是不可变的类型,字符串的内建函数有非常多,我们一一举例来看看他们的作用

下面是用dir(str) 输出的内容:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

  

1.capitalize  把字符串的第一个字符大写

2.casefold python3中新添加的,具体作用是给其它除英语以外的语言改成小写。(跳过

3.center 居中

s = "this is string example....wow!!!"
print ("str.center(40, 'a') : ", str.center(40, 'a'))   # 此处40代表的是输出结果的字符串长度,‘a’指的是填充物,若没有这个参数,默认填充空格 >>>str.center(40, 'a') : aaaathis is string example....wow!!!aaaa

4.count 不解释

5.encode 以encode指定的编码格式编码string

s = u'djc'      # s 是unicode
s.encode(encoding='UTF-8',errors='ignore') # 此处把s转变成了UTF-8的形式,后面的errors 可以是ignore 或者 replace,也可以不填,那样的话如果出错会报ValueError
print(s)

6.endswith 检查字符串尾部  对应的有一个startswith 检查字符串是否以xxx开头,和endswith类似

s = 'djc is a asshole'
v = s.endswith('e',3,9)    # 检测字符串在3到9的范围内是否是以e结尾,很显然是False
print(v) >>>False

7.expandtabs 把字符串中的tab符号转换成空格,默认空格数tabsize = 8

8.find

s = 'djc is a asshole'
v = s.find('m')      # 检测 m 是否包含在 s 中,若不在返回-1,若在返回该字符所在位置位置,如果是一串字符,则返回第一个字符所在位置
print(v) >>>-1

9.format

name = 'deng {0} is {1}'
print(name.format('asshole','sad')) # 字符串的拼接,这个函数的语法是把asshole和sad赋值给{0}和{1},这也是字符串格式化的一种
name = 'deng {name} is {id}'
print(name.format(name = 'asshole',id = 'sad'))

10.format_map 现在我也不知道,日后再来补充

11.index 和find功能一样,区别在于find找不到会返回-1,而index会报错

12.isalnum

s = 'djc.1'
v = s.isalnum()      # 如果字符串中至少有一个字符并且所有字符都是数字或字母则返回Ture,否则返回False
print(v) >>>False

13.isalpha 和上面的类似,只不过是只有当全部是字符是就会返回Ture

14.isdecimal string中只包含10进制数则返回Ture,否则返回False

15.isdigit string中只包含数字则返回Ture,否则返回False

16.isidentifier  string中是否是关键字是则返回Ture,否则返回False

17.islower

s = 'sdasd123'
v = s.islower()      # string中包含一种区分大小写的字符,并且这些字符都是小写,则返回Ture,否则返回False,因为数字不区分大小写,所以不影响结果 print(v)
>>> Ture

18.isnumeric 和isdight功能,全是数字则返回Ture,否则返回Flse

19.isprintable 跳过

20.isspace string中如果只包含空格,则返回Ture,否则返回False

21.istitle string中如果是标题化的则返回Ture,否则是False。标题化见title()

22.isupper 和islower相反,全是大写则返回Ture,否则是False

23.join

#对序列进行操作(分别使用' '与':'作为分隔符)

>>> seq1 = ['hello','good','boy','doiido']
>>> print ' '.join(seq1)
hello good boy doiido
>>> print ':'.join(seq1)
hello:good:boy:doiido s = 'hello good boy doiido'
>>> print ':'.join(seq1.split(sep = ' ')      # split() 参数sep = ‘ ’,以空格分割字符串s
hello:good:boy:doiido #对字符串进行操作 >>> seq2 = "hello good boy doiido"
>>> print (':'.join(seq2))
h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o
>>> print #对元组进行操作 >>> seq3 = ('hello','good','boy','doiido')
>>> print (':'.join(seq3))
hello:good:boy:doiido #对字典进行操作 >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print (':'.join(seq4)
boy:good:doiido:hello #合并目录 >>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'

24.ljust

str = "djc is a asshole"
print (str.ljust(50, '0'))      # 返回一个原字符串左对齐,默认用空格填充 >>>djc is a asshole0000000000000000000000000000000000

25.rjust 跟上面一样,只不过是右对齐

26.lower 将string中的大写字符全部转换成小写

27.upper 和lower相反

28.swapcase 翻转string中的大小写

29.lstrip 截掉string左边的空格

s = "             djc is a asshole"
v = s.lstrip()
print(v) >>>djc is a asshole

30.maketrans

语法: str.maketrans(intab, outtab]);         # 详细见http://blog.csdn.net/u014351782/article/details/46740297

Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,
第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
注:两个字符串的长度必须相同,为一一对应的关系。 Python3.4已经没有string.maketrans()了,取而代之的是内建函数:
bytearray.maketrans()、bytes.maketrans()、str.maketrans()
""" intab = "abcd"
outtab = "1234"
str_trantab = str.maketrans(intab,outtab) test_str = "csdn blog: http://blog.csdn.net/wirelessqa" print (test_str.translate(str_trantab)) >>>3s4n 2log: http://2log.3s4n.net/wirelessq1

31.partition 返回一个三元tuple  rpartition 从右边开始查找,和partition类似

s = 'djc is a asshole'
v = s.partition('ass')      # 返回一个参数左边,参数,参数右边组成的三元元祖
print(v) >>>('djc is a ', 'ass', 'hole')

32.replace 替代

s = 'djc is a asshole'
v = s.replace('s','d',1) # 把s字符串中的s替换成d,最后那个参数表示最多替换多少次,此处为一次因此只换了一次
print(v) >>>djc id a asshole

33.rfind 和find类似,不过是从右边开始查找

34.rindex 和index类似,不过是从右边开始索引

35.split

s = 'djc is a asshole'
v = s.split(' ',2)      # 以空格为分隔符,分割两次,默认为有多少分多少
print(v) >>>['djc', 'is', 'a asshole']

36.splitlines 分割行,字符串中有\n就\n这分割,相当于split('\n')

37.strip 删除字符串左右两边的空格

38.title 返回标题化的string,就是说所有的单词都是以大写开始,其余字母都是小写

39.translate 与maketrans一起使用,详情见30

40.zfill 返回长度为width的字符串,原字符串string右对齐,前面填充0

a = 'djc is a asshole'
v = a.zfill(40)
print(v) >>>000000000000000000000000djc is a asshole

好多QAQ

String bulit-in function的更多相关文章

  1. 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}

    应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff] ...

  2. String类型,Function类型

    1.String类型:  1)创建String对象:    var str=new String(s);    String(s);    参数:参数 s 是要存储在 String 对象中的值或转换成 ...

  3. c++中string::function集合

    string append() 1.直接添加另一个完整的字符串: str1.append(str2); 2.添加另一个字符串的某一段字串:    str1.append(str2, 11,  7); ...

  4. 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)

    输出函数:这个函数别看它小,但浓缩的都是精华啊 作用:对于高精度的数组进行倒序输出 思路:首先从被传入的数组第一位开始,一直往前扫输出就可以了(i--) 注释:因为每个数组的第一位是用来存储这个数组的 ...

  5. 【JavaScript框架封装】使用Prototype给Array,String,Function对象的方法扩充

    /* * @Author: 我爱科技论坛* @Time: 20180705 * @Desc: 实现一个类似于JQuery功能的框架* V 1.0: 实现了基础框架.事件框架.CSS框架.属性框架.内容 ...

  6. 常用function() 收集

    1.随机数生成函数(来源-微信支付demo案例) /** * * 产生随机字符串,不长于32位 * @param int $length * @return 产生的随机字符串 */ public st ...

  7. Delphi在创建和使用DLL的时候如果使用到string,请引入ShareMem单元

    当使用了长字符串类型的参数.变量时,如string,要引用ShareMem. 虽然Delphi中的string功能很强大,但若是您编写的Dll文件要供其它编程语言调用时,最好使用PChar类型.如果您 ...

  8. Js实现string.format

    经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...

  9. String对象方法扩展

    /** *字符串-格式化 */ String.prototype.format = function(){ var args = arguments;//获取函数传递参数数组,以便在replace回调 ...

  10. jQuery extend扩展String原型

    jQuery.extend(String.prototype, { isPositiveInteger:function(){ return (new RegExp(/^[1-9]\d*$/).tes ...

随机推荐

  1. 洛谷P2365 任务安排 [解法一]

    题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始 ...

  2. *AtCoder Regular Contest 096E - Everything on It

    $n \leq 3000$个酱,丢进拉面里,需要没两碗面的酱一样,并且每个酱至少出现两次,面的数量随意.问方案数.对一给定质数取模. 没法dp就大力容斥辣.. $Ans=\sum_{i=0}^n (- ...

  3. AC日记——[USACO1.1]坏掉的项链Broken Necklace 洛谷 P1203

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  4. (1)Swing创建窗体

    本系列使用Intellij IDEA 2017.3.4版本 一.运行窗体 1. 2. 3. 4. 5. 6. 给JPanel起个名字 -如From 7. 8. 9. 生成 import javax.s ...

  5. BZOJ3786 星际探索

    @(BZOJ)[DFS序, Splay] Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其 ...

  6. go语言学习之路 二:变量

    说道变量,首先应该提一提关键字,因为不能把关键字当做变量来声明. 关键字: 下面列出GO语言的关键字或保留字: break default func interface select case def ...

  7. cms完整视频教程+源码 孔浩老师 全131讲

    话不多说,请看如下链接,  项目在此文件夹目录下:  JAVA专区>3.深入Java Web>3.1.cms项目 很多反馈说无效 本次 2016.09.12 发布最新链接 链接:https ...

  8. Sentinel实现Redis高可用

    实现目标: 一主两从,集群起始VIP在master上边,如果当前master挂了,sentinel自动选出一个slave当选master,并把VIP漂移到这台机器,然后把另一台slave指向的mast ...

  9. [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)

    Do you get lost when working with functions and the new keyword? Prototypal inheritance can be compl ...

  10. 搭建企业内部DNS服务器,docker 部署内部 dnsmasq

    获取镜像 docker pull jpillora/dnsmasq 配置域名 # http://oss.segetech.com/intra/srv/dnsmasq.conf #log all dns ...