Python之字符(2)

1、string.issupper()表示判断字符是否全部为小写字母。
 1 string1 = "abcdef"
 2 string2 = "ABCdef"
 3 string3 = "ABCDEF"
 4 print(string1.isupper())
 5 print(string2.isupper())
 6 print(string3.isupper())
 7 结果为:
 8 False
 9 False
10 True
2、string.join(seq)用“string”字符将seq连接为一个字符串,seq可以为list,tuple,dirct,string类型,例如
1 January+Febrary+Match+April+May+Jane+July+Augest
2 January#Febrary#Match#April#May#Jane#July#Augest
3 January*Febrary*Match*April*May*Jane*July*Augest
4 结果为:
5 January+Febrary+Match+April+May+Jane+July+Augest
6 January#Febrary#Match#April#May#Jane#July#Augest
7 January*Febrary*Match*April*May*Jane*July*Augest
3、string.ljust(参数1,参数2)表示左对齐,参数1表示总得字符宽,当string的宽度小于参数1给定的长度时,则右侧以参数2填充     string.rjust()表示右对齐,当参数string的宽度小于参数1时,左侧用参数2填充。当string的长度大于参数1时,什么也不做。
 1 a = "Hello World"
 2 print(a.ljust(15,'-'))
 3 print(a.rjust(15,'+'))
 4 print(a.ljust(10,'='))
 5 print(a.ljust(10,'&'))
 6 结果为:
 7 Hello World----
 8 ++++Hello World
 9 Hello World
10 Hello World
4、string.lower()表示将string变为小写字符。
 1 a = "abcdefg"
 2 print(a.lower())
 3 b = "ABCDEFG"
 4 print(a.lower())
 5 c = "ABCdefg"
 6 print(c.lower())
 7 结果为:
 8 abcdefg
 9 abcdefg
10 abcdefg
5、string.lstrip()表示修剪string左侧的回车和空格字符      string.rstrip()表示修剪string右侧的回车和空格字符      string.strip()表示修剪string左右两侧的回车和空格字符
1 string_1 = "    abcdefg     "
2 print(string_1.lstrip())
3 print(string_1.rstrip())
4 print(string_1.strip())
5 结果为:
6 abcdefg     
7     abcdefg
8 abcdefg
6、trantab = str.maketrans(intab,outab)表示将intab转化为相应的outab,string.translate(trantab)表示按trantab的映射方法翻译string的内容,如果intab和outab没有相对应的映射,则保留string中原有的形式。
1 a = 'abcdefg'
2 b = '1234567'
3 string = "This is a beautiful world , Welcome!!"
4 trantab = str.maketrans(a,b)
5 trans = string.translate(trantab)
6 print(trans)
7 结果为:
8 This is 1 251uti6ul worl4 , W5l3om5!!
7、string.partition(参数1)表示将字符串string按参数1的分隔符分隔,生成一个元组,且此元组的元素个数为2
1 string = "www.baidu.com"
2 str_tuple = string.partition('.')
3 print(str_tuple)
4 结果为:
5 ('www', '.', 'baidu.com')
注:由于元组的元素个数为3,所以“baidu.com”的这个“ . ”没有分开 8、string.repalce(old,new,max)用新的字符替换旧的字符,最多替换次数max
1 string = "this is a wonderful world, this is the first time to meet Python"
2 print(string.replace('is','was',3))
3 结果为:
4 thwas was a wonderful world, thwas is the first time to meet Python
9、string.split(参数1,参数2)将string按参数1的分隔符分隔参数2的次数
1 string = "this is why Python is wonderful!"
2 print(string.split(' '))
3 print(string.split(' ',3))
4 结果为:
5 ['this', 'is', 'why', 'Python', 'is', 'wonderful!']
6 ['this', 'is', 'why', 'Python is wonderful!']
注:此和序号为7的partition()不同,split生成的是一个list,partition生成的是一个元组且元素数为3,split则没有限制10、string.splitlines(参数)表示按换行符和回车符为分隔符,生成一个list,当没有参数是只是按"t","r"分隔,当参数为True时,则按"t","r"分隔,且保留"t","r"
1 string = "ab cnnde fgrklrn"
2 str_1 = string.splitlines()
3 str_2 = string.splitlines(True)
4 print(str_1)
5 print(str_2)
6 结果为:
7 ['ab c', '', 'de fg', 'kl']
8 ['ab cn', 'n', 'de fgr', 'klrn']
11、string.startwith(str,参数1,参数2)判断字符串string是否是以str开始的,参数1表示开始判断的位置,参数2表示结束的位置
 1 string = "this is a wonderful world!"
 2 str_1 = string.startswith('this')
 3 str_2 = string.startswith('th')     
 4 str_3 = string.startswith('is',2,5)  
 5 str_4 = string.startswith('is',3,5)      #虽然str_1、str_2、str_3均为True,但是str_4为False,说明是按空格分隔后才比的,否则str_4应为True。 7 print(str_1)
 8 print(str_2)
 9 print(str_3)
10 print(str_4)12 结果为:
13 True
14 True
15 False
16 False
17 False
12、string.swapcase()将大小写字母相互转换
1 a = "This Is a Wonderful World!"
2 print(a.swapcase())
3 结果为:
4 tHIS iS A wONDERFUL wORLD!
13、string.title()将字符string转化为标题类型
1 a = "this is a wonderful world!"
2 print(a.title())
3 结果为:
4 This Is A Wonderful World!
14、upper()转化为大写
1 a = "this is a wonderful world!"
2 print(a.upper())
3 结果为:
4 THIS IS A WONDERFUL WORLD!
15、string.zfill()返回指定长度的字符串,原字符串右对齐,不足则在前边不“0”
1 a = "this is a wonderful world!"
2 print(a.zfill(5))
3 print(a.zfill(40))
4 print(a.zfill(50))
5 结果为:
6 this is a wonderful world!
7 00000000000000this is a wonderful world!
8 000000000000000000000000this is a wonderful world!

相关内容推荐