capitalize() 方法返回一个字符串的copy,并且这个字符串的首字母大写。例如:
str = "this is string example....wow!!!"; print "str.capitalize() : ", str.capitalize() #output result str.capitalize() : This is string example....wow!!!
count() 方法返回子串在指定范围内出现的次数。例如:
str.count(sub, start=0,end=len(string))
str = "this is string example....wow!!!"; sub = "i"; print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40) sub = "wow"; print "str.count(sub) : ", str.count(sub)
#output result str.count(sub, 4, 40) : 2 str.count(sub, 4, 40) : 1
endswith() 判断在指定范围内,是否以子串结束。例如:
startswith()
str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith(suffix); print str.endswith(suffix,20); suffix = "is"; print str.endswith(suffix, 2, 4); print str.endswith(suffix, 2, 6); #output result True True True False
find() 返回子串在指定范围内首次出现的位置,未查到返回-1。例如:
str.find(str, beg=0,end=len(string))
str1 = "this is string example....wow!!!"; str2 = "exam"; print str1.find(str2); print str1.find(str2, 10); print str1.find(str2, 40); #result 15 15 -1
index()返回子串在指定范围内首次出现的位置,未查到抛出异常。例如:
str.index(str, beg=0end=len(string))
str = "this is string example....wow!!!"; str = "exam"; print str.index(str); print str.index(str, 10); print str.index(str, 40); #result 15 15 Traceback (most recent call last): File "test.py", line 8, in print str.index(str, 40); ValueError: substring not foundisalnum()判断字符串是否全是字母和数字(要么全是字母,要么全是数字,要么全是数字和字母)例如:
str.isa1num()
str = "this2009"; # No space in this string print str.isalnum(); str = "this is string example....wow!!!"; print str.isalnum(); #result True Falseisalpha()方法判断字符串内容全是字母。例如:
str.isalpha()
str = "this"; # No space & digit in this string print str.isalpha(); str = "this is string example....wow!!!"; print str.isalpha(); #result True Falseisdecimal()和isnumeric()判断字符串是否全是数字,该字符串必须是unicode object。例如:
str.isdecimal()
str = u"this2009"; print str.isdecimal(); str = u"23443434"; print str.isdecimal(); #result False Trueisdigit()判断字符串全部为数字。例如:
str.isdigit()
str = "123456"; # Only digit in this string print str.isdigit(); str = "this is string example....wow!!!"; print str.isdigit(); #result True Falseislower()判断字符串中所有的字母是否都是小写。 isupper() 判断字符串中所有的字母是否都是大写。例如:
str.islower()
str = "THIS is string example....wow!!!"; print str.islower(); str = "this is string example....wow!!!"; print str.islower(); #result False True
isspace()判断字符串是否全是空白符,例如:
str.isspace()
str = " \t\n"; #include tab,space print str.isspace(); str = "This is string example....wow!!!"; print str.isspace(); #result True False
istitle()判断字符串中,每个单词的首字母是否都是大写。例如:
str.istitle()
str = "This Is String Example...Wow!!!"; print str.istitle(); str = "This is string example....wow!!!"; print str.istitle(); #result True False
join()通过特殊字符把字符串连接起来,例如:
str.join(sequence)
str = "-"; seq = ("a", "b", "c"); # This is sequence of strings. print str.join( seq ); #result a-b-c
len(str) 计算字符串的长度。
str.lower()把所有的大写字母转成小写。
str.upper()把所有的小写字母转成大写。
swapcase() 方法是把字符串中的小写转成大写,大写转成小写。例如
str.swapcase();
str = "this is string example....wow!!!"; print str.swapcase(); str = "THIS IS STRING EXAMPLE....WOW!!!"; print str.swapcase(); #result THIS IS STRING EXAMPLE....WOW!!! this is string example....wow!!!
lstrip()去除掉字符串左边规定的字符,默认是空格。例如:
rstrip()去除掉字符串右边规定的字符,默认是空格。
strip()去除掉两边规定的字符,默认是空格
str.rstrip([chars])
str.lstrip([chars])
str.strip([chars]);
str = " this is string example....wow!!! "; print str.lstrip(); str = "88888888this is string example....wow!!!8888888"; print str.lstrip('8'); #result this is string example....wow!!! this is string example....wow!!!8888888
str = " this is string example....wow!!! "; print str.rstrip(); str = "88888888this is string example....wow!!!8888888"; print str.rstrip('8'); #result this is string example....wow!!! 88888888this is string example....wow!!!
maketrans()看例子吧:例子中实际上是把对应的字母替换成数字。
str.maketrans(intab, outtab]);
from string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow!!!"; print str.translate(trantab); #result th3s 3s str3ng 2x1mpl2....w4w!!!
max()返回字符串中最大的字母。例如:
max(str)
str = "this is really a string example....wow!!!"; print "Max character: " + max(str); str = "this is a string example....wow!!!"; print "Max character: " + max(str); #result Max character: y Max character: x
replace()用新字符替换旧字符
str.replace(old,new[, max]) max表示替换的个数
str = "this is string example....wow!!! this is really string"; print str.replace("is", "was"); print str.replace("is", "was", 3); #result thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string
rfind()返回指定指定范围内,子串最后出现的索引,找不到返回-1。例如:
str.rfind(str, beg=0end=len(string))
str = "this is really a string example....wow!!!"; str1 = "is"; print str.rfind(str1); print str.rfind(str1, 0, 10); print str.rfind(str1, 10, 0); print str.find(str1); print str.find(str1, 0, 10); print str.find(str1, 10, 0); #result 5 5 -1 2 2 -1
rjust()看例子吧:
str.rjust(width[, fillchar])
str = "this is string example....wow!!!"; print str.rjust(50, '0'); #result 000000000000000000this is string example....wow!!!
zfill()用“0”进行填充。看例子吧:
str.zfill(width)
str = "this is string example....wow!!!"; print str.zfill(40); print str.zfill(50); #result 00000000this is string example....wow!!! 000000000000000000this is string example....wow!!!
split()按指定的分隔符分隔字符串,最终返回一个列表。例如:
str.split(str="", num=string.count(str)).num代表分隔的次数
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); print str.split(' ', 1 ); #result ['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
title() 把字符串中每个单词的首字母大写。例如:
str.title();
str = "this is string example....wow!!!"; print str.title(); #result This Is String Example....Wow!
translate()看例子吧
str.translate(table[, deletechars]);
from string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow!!!"; print str.translate(trantab, 'xm'); #result th3s 3s str3ng 21pl2....w4w!!!
相关推荐
Python字符串常用方法汇总.docxPython字符串常用方法汇总.docxPython字符串常用方法汇总.docxPython字符串常用方法汇总.docxPython字符串常用方法汇总.docxPython字符串常用方法汇总.docxPython字符串常用方法汇总....
字符串的常用方法如下表: 常用方法 说明 find() 字符串查找 count() 字符串出现次数 split() 分割字符串 join() 拼接字符串 replace() 字符串替换 strip() 移除字符串中的空格和指定字符 字符串常用方法 find()方法...
python中字符串方法 Python中的字符串方法是非常强大的,它们可以帮助我们处理和操作字符串。在本文中,我们将介绍一些常用的字符串方法,包括字符串的拼接、替换、查找、分割、大小写转换等。 1. 字符串的拼接 字符...
在本文中,我们将探讨如何使用Python中的字符串反转方法来反转字符串。 Python中的字符串是一系列字符的序列,可以使用单引号或双引号来表示。例如,以下是一个字符串: ``` my_string = "Hello, World!" ``` 要反转...
主要介绍了C语言字符串转换为Python字符串的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
python字符串学习笔记.python字符串操作方法
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换、删除、截取、复制、连接、比较、查找、分割等,需要的朋友可以参考下。
输入一个字符串,分别统计出其中英文字母、空格、数字和其它字符的个数,本文给出解决方法 编写思路: 1、字符串的遍历,和列表类似,可以把字符串当做元素都是一个字符的一个字符列表,它可以和列表有公共的语法 2...
Python字符串调用方法解释(部分),因为本人使用的是3.1版,很多旧版的调用方法新版不支持,所以只有些基础的调用和代码运行的截图,比较适合和我一样的菜鸟级的童鞋观摩。
python字符串的使用方法 Python字符串的使用方法 Python是一种高级编程语言,它支持多种数据类型,其中字符串是最常用的数据类型之一。字符串是由一系列字符组成的,可以包含字母、数字、符号等。在Python中,字符串...
python3 实现js中JSEncrypt encrypt方法,rsa模块根据字符串公钥生成加密字符串 使用时直接调用rsa_encrypt(s, pubkey_str)方法就好了,第一个参数为待加密字符串,第二个参数为公钥,返回值为加密后的字符串 其中_...
本文实例讲述了python转换字符串为摩尔斯电码的方法。分享给大家供大家参考。具体实现方法如下: chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz" codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ......
利用Python实现字符串的逆向读取 #通过空格将字符串分隔符,把各个单词分隔为列表 # 翻转字符串 假设列表 list = [1,2,3,4], list[0]=1, list[1]=2 ,而 -1 表示最后一个元素 list[-1]=4 ( 与 list[3]=4 ...
本文实例讲述了python判断给定的字符串是否是有效日期的方法。分享给大家供大家参考。具体分析如下: 这里python判断给定的字符串是否是一个有效的日期,如果是一个日期格式的字符串,该函数返回True,否则返回False...
python 字符串替换 是python 操作字符串的时候经常会碰到的问题,这里简单介绍下字符串替换方法。 python 字符串替换可以用2种方法实现: 1是用字符串本身的方法。 2用正则来替换字符串 下面用个例子来实验下: a = ...
您可能感兴趣的文章:python中字符串的操作方法大全python 遍历字符串(含汉字)实例详解Python实现简单文本字符串处理的方法Python 打印中文字符的三种方法Python实现针对含中文字符串的截取功能示例Python 字符串...
《Python字符串拼接方法详解.docx》讲解很详细,很有用
Python3倒序输出字符串的N种方法,介绍了包括直接reverse方法,for循环正序和逆序法等三种方法来进行字符串倒序
python字符串, 列表, 字典, 集合方法说明