python的一些内置函数

测试用字符串

s1 = 'hijKLMNabcDEFg1234'
s2 = 'hello'
s3 = '哈哈哈哈哈哈哈哈'
s4 = '哈看dd'
s5 = '12345'
s6 = '好好12345'
s7 = '\n\t'
s8 = '   '
s9 = 'HelLo World'
s10 = '    helloworld  '
s11 = '哈哈hei77'
s12='关羽,张飞,任小粟,8 hello'
s13 = 'ab\n哈哈\n\t123\n'
s14 = '  hello'

有关大小写的函数

#1 首字母大写,剩下的小写
 print('s1.capitalize()',s1.capitalize())
 //s1.capitalize() Hijklmnabcdefg1234
#2 转换字符串中所有大写字母为小写
 print('s9.lower',s9.lower())
#3转换字符串中所有小写字母为大写
 print('s9.upper',s9.upper())
#4 返回“标题化”的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
#与capitalize基本相同但title的每个单词要大写(以空格为界)
 print('s1.title',s1.title())
 print('s12.title',s12.title())
//s1.title Hijklmnabcdefg1234	
//s12.title 关羽,张飞,任小粟,8 Hello  
#5 英语方面用法和lower相同
 print('s1.casefold()',s1.casefold())
 //s1.casefold() hijklmnabcdefg1234
#6 将字符串中大写转换为小写,小写转换为大写
 print('s1.swapcase',s1.swapcase())
 //s1.swapcase HIJklmnABCdefG1234

有关切割的函数

# 1um=string.count(str)) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num 个子字符串。
 print('s12.split',s12.split(','))
 //s12.split ['关羽', '张飞', '任小粟', '8 hello']
#2 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符
 print("s13.splitlines",s13.splitlines(False))
 //s13.splitlines ['ab', '哈哈', '\t123']
#3截掉字符串左边的空格
 print('s10.lstrip',s10.lstrip())
 //s10.lstrip helloworld
#4 删除字符串末尾的空格
 print('s10.rstrip',s10.rstrip())
 //s10.rstrip     helloworld
#5 在字符串上执行 lstrip()和 rstrip()
 print('s10.strip',s10.strip())
posted @ 2020-02-26 11:27  茕祇  阅读(38)  评论(0编辑  收藏  举报