[摘抄]Python内置的字符串处理函数整理

str=‘python String function‘

生成字符串变量str=‘python String function‘

字符串长度获取:len(str)
例:print ‘%s length=%d‘ % (str,len(str))

字母处理
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写,其余小写:str.capitalize()
首字母大写:str.title()
print ‘%s lower=%s‘ % (str,str.lower())
print ‘%s upper=%s‘ % (str,str.upper())
print ‘%s swapcase=%s‘ % (str,str.swapcase())
print ‘%s capitalize=%s‘ % (str,str.capitalize())
print ‘%s title=%s‘ % (str,str.title()) 
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
print ‘%s ljust=%s‘ % (str,str.ljust(20))
print ‘%s rjust=%s‘ % (str,str.rjust(20))
print ‘%s center=%s‘ % (str,str.center(20))
print ‘%s zfill=%s‘ % (str,str.zfill(20))

字符串搜索相关
搜索指定字符串,没有返回-1:str.find(‘t‘)
指定起始位置搜索:str.find(‘t‘,start)
指定起始及结束位置搜索:str.find(‘t‘,start,end)
从右边开始查找:str.rfind(‘t‘)
搜索到多少个指定字符串:str.count(‘t‘)
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
print ‘%s find nono=%d‘ % (str,str.find(‘nono‘))
print ‘%s find t=%d‘ % (str,str.find(‘t‘))
print ‘%s find t from %d=%d‘ % (str,1,str.find(‘t‘,1))
print ‘%s find t from %d to %d=%d‘ % (str,1,2,str.find(‘t‘,1,2))
#print ‘%s index nono ‘ % (str,str.index(‘nono‘,1,2))
print ‘%s rfind t=%d‘ % (str,str.rfind(‘t‘))
print ‘%s count t=%d‘ % (str,str.count(‘t‘))

字符串替换相关
替换old为new:str.replace(‘old‘,‘new‘)
替换指定次数的old为new:str.replace(‘old‘,‘new‘,maxReplaceTimes)
print ‘%s replace t to *=%s‘ % (str,str.replace(‘t‘, ‘*‘))
print ‘%s replace t to *=%s‘ % (str,str.replace(‘t‘, ‘*‘,1))

字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip(‘d‘),相应的也有lstrip,rstrip
str=‘ python String function ‘
print ‘%s strip=%s‘ % (str,str.strip())
str=‘python String function‘
print ‘%s strip=%s‘ % (str,str.strip(‘d‘))

按指定字符分割字符串为数组:str.split(‘ ‘)

默认按空格分隔
str=‘a b c de‘
print ‘%s strip=%s‘ % (str,str.split())
str=‘a-b-c-de‘
print ‘%s strip=%s‘ % (str,str.split(‘-‘))

字符串判断相关
是否以start开头:str.startswith(‘start‘)
是否以end结尾:str.endswith(‘end‘)
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
str=‘python String function‘
print ‘%s startwith t=%s‘ % (str,str.startswith(‘t‘))
print ‘%s endwith d=%s‘ % (str,str.endswith(‘d‘))
print ‘%s isalnum=%s‘ % (str,str.isalnum())
str=‘pythonStringfunction‘
print ‘%s isalnum=%s‘ % (str,str.isalnum())
print ‘%s isalpha=%s‘ % (str,str.isalpha())
print ‘%s isupper=%s‘ % (str,str.isupper())
print ‘%s islower=%s‘ % (str,str.islower())
print ‘%s isdigit=%s‘ % (str,str.isdigit())
str=‘3423‘
print ‘%s isdigit=%s‘ % (str,str.isdigit())

还有其他常见的Python字符串处理 函数的话不定期更新。

原文地址:http://www.jb51.net/article/33631.htm

时间: 2024-12-14 18:15:02

[摘抄]Python内置的字符串处理函数整理的相关文章

Python内置的字符串处理函数整理

Python内置的字符串处理函数整理 作者: 字体:[增加 减小] 类型:转载 时间:2013-01-29我要评论 Python内置的字符串处理函数整理,收集常用的Python 内置的各种字符串处理 函数的使用方法 str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length=%d' % (str,len(str)) 字母处理全部大写:str.upper()全

Python内置的字符串处理函数

生成字符串变量 str='python String function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符串 sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr1 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 比

PHP内置的字符串处理函数

字符串的特点    1.其他类型的数据用在字符串类型处理函数中,会自动将其转化成字符串后,在处理 <?php echo substr("abcdefghijklmn",2,4),"<br>"; //cdef //使用数字会自动转化为字符串 echo substr(123456,2,4); //3456 ?> 2.可以将字符串视为数组,当做字符集合来看待 <?php $str="abcdefg"; //下面这两种方法都

python内置常用高阶函数(列出了5个常用的)

原文使用的是python2,现修改为python3,全部都实际输出过,可以运行. 引用自:http://www.cnblogs.com/duyaya/p/8562898.html https://blog.csdn.net/cv_you/article/details/70880405 python内置常用高阶函数: 一.函数式编程 •函数本身可以赋值给变量,赋值后变量为函数: •允许将函数本身作为参数传入另一个函数: •允许返回一个函数. 1.map()函数 是 Python 内置的高阶函数,

python内置对象---字符串类型

字符串是一个有序的字符的集合. 一.字符串常量 编写方法: 单引号:'f"uck' 双引号:"f'uck" 三引号:"""fuck""",'''fuck''' 转义字符:"f\tc\nuc\0k" Raw字符串:r"C:\new\test.spm" Byte字符串:b'fu\x01uk' 以下是使用字符串常量表达式创建 实例    1.单双引号是一样的 >>>

Python 内置的一些高效率函数用法

1.  filter(function,sequence) 将sequence中的每个元素,依次传进function函数(可以自定义,返回的结果是True或者False)筛选,返回符合条件的元素,重组成一个String,List,Tuple等(跟sequence一样) 示例 def func(x): return x%2==0 and x%3==0 filter(func,(3,6,8,12,15,21)) #(6, 12) 2. map(functiom,sequence) 将sequence

Python内置函数_数学运算类

本文和大家分享的主要是python内置函数数据运算类相关内容,一起来看看吧,希望对大家学习python 有所帮助. abs abs(x) 求绝对值 · X可以是整型,也可以是复数 · 若X是复数,则返回复数的模 >>> abs(-1)1>>> abs(-3+4j)5.0>>> bin bin(x) 将整数x转换为二进制字符串 >>> bin(2)'0b10'>>> bin(3)'0b11' bool bool([x]

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a

Python补充03 Python内置函数清单

作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数: 基本数据类型 type() 反过头来看看 dir() help() len() 词典 len() 文本文件的输入输出 op