python字符串函数
来源:互联网 发布:java中单引号转义 编辑:程序博客网 时间:2024/06/07 01:43
去空格及特殊符号
s.strip().lstrip().rstrip(',')
复制字符串
#strcpy(sStr1,sStr2)sStr1 = 'strcpy'sStr2 = sStr1sStr1 = 'strcpy2'print sStr2
连接字符串
#strcat(sStr1,sStr2)sStr1 = 'strcat'sStr2 = 'append'sStr1 += sStr2print sStr1
查找字符
#strchr(sStr1,sStr2)# < 0 为未找到sStr1 = 'strchr'sStr2 = 's'nPos = sStr1.index(sStr2)print nPos
比较字符串
#strcmp(sStr1,sStr2)sStr1 = 'strchr'sStr2 = 'strch'print cmp(sStr1,sStr2)
扫描字符串是否包含指定的字符
#strspn(sStr1,sStr2)sStr1 = '12345678'sStr2 = '456'#sStr1 and chars both in sStr1 and sStr2print len(sStr1 and sStr2)
字符串长度
#strlen(sStr1)sStr1 = 'strlen'print len(sStr1)
将字符串中的大小写转换
#strlwr(sStr1)sStr1 = 'JCstrlwr'sStr1 = sStr1.upper()#sStr1 = sStr1.lower()print sStr1
追加指定长度的字符串
#strncat(sStr1,sStr2,n)sStr1 = '12345'sStr2 = 'abcdef'n = 3sStr1 += sStr2[0:n]print sStr1
字符串指定长度比较
#strncmp(sStr1,sStr2,n)sStr1 = '12345'sStr2 = '123bc'n = 3print cmp(sStr1[0:n],sStr2[0:n])
复制指定长度的字符
#strncpy(sStr1,sStr2,n)sStr1 = ''sStr2 = '12345'n = 3sStr1 = sStr2[0:n]print sStr1
将字符串前n个字符替换为指定的字符
#strnset(sStr1,ch,n)sStr1 = '12345'ch = 'r'n = 3sStr1 = n * ch + sStr1[3:]print sStr1
扫描字符串
#strpbrk(sStr1,sStr2)sStr1 = 'cekjgdklab'sStr2 = 'gka'nPos = -1for c in sStr1: if c in sStr2: nPos = sStr1.index(c) breakprint nPos
翻转字符串
#strrev(sStr1)sStr1 = 'abcdefg'sStr1 = sStr1[::-1]print sStr1
查找字符串
#strstr(sStr1,sStr2)sStr1 = 'abcdefg'sStr2 = 'cde'print sStr1.find(sStr2)
分割字符串
#strtok(sStr1,sStr2)sStr1 = 'ab,cde,fgh,ijk'sStr2 = ','sStr1 = sStr1[sStr1.find(sStr2) + 1:]print sStr1#或者s = 'ab,cde,fgh,ijk'print(s.split(','))
连接字符串
delimiter = ','mylist = ['Brazil', 'Russia', 'India', 'China']print delimiter.join(mylist)PHP 中 addslashes 的实现def addslashes(s): d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"} return ''.join(d.get(c, c) for c in s)s = "John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0"print sprint addslashes(s)
只显示字母与数字
def OnlyCharNum(s,oth=''): s2 = s.lower(); fomart = 'abcdefghijklmnopqrstuvwxyz0123456789' for c in s2: if not c in fomart: s = s.replace(c,''); return s;print(OnlyStr("a000 aa-b"))
截取字符串
str = ’0123456789′print str[0:3] #截取第一位到第三位的字符print str[:] #截取字符串的全部字符print str[6:] #截取第七个字符到结尾print str[:-3] #截取从头开始到倒数第三个字符之前print str[2] #截取第三个字符print str[-1] #截取倒数第一个字符print str[::-1] #创造一个与原字符串顺序相反的字符串print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符print str[-3:] #截取倒数第三位到结尾print str[:-5:-3] #逆序截取,具体啥意思没搞明白?
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 字符串处理函数
- python字符串处理函数
- PYTHON-字符串处理函数
- Python字符串处理函数
- Python字符串处理函数
- Python:字符串函数
- PYTHON字符串常用函数
- python 字符串操作函数
- python字符串处理函数
- Python:字符串函数
- python字符串函数归纳
- Python字符串分割函数
- python 字符串处理函数
- python字符串函数
- Python字符串常用函数
- Python字符串处理函数
- python 字符串操作函数
- python 字符串函数
- logback 配置详解(一)
- Zabbix部署
- mac 安装redis & 关闭保护模式
- 华为秋招2017年8月笔试
- JS设置checkbox 为选中和未选中状态
- python字符串函数
- iOS 实现长屏截图,scrollview,tableview截图
- css怎么将一张图片在另一个元素中垂直居中
- 购物车多sku组合查询算法
- Android安全开发之安全使用HTTPS
- Ubuntu 14.04 搭建CDH5*笔记
- TCP/IP协议-应用层协议端口号及各层协议数据单元
- linux的网络配置有线线缆被拔出
- STL(六)Vector向量容器