字符串内置函数--str(object)

 
str
test = 'alex'
###首字母大写
v1 = test.capitalize()
###所有变小写,caseflod更牛逼,很多未知的对应变小写
v2 = test.casefold()
v3 = test.lower()
print(v1,v2,v3)
###设置宽度,并将内容居中
v4 =  test.center(20,'*')
print(v4)
###去字符串中寻找子序列出现的个数
v5 = test.count('e',1,6)
print(v5)
###判断是否以指定子序列结尾(开头),返回True False
v6 = test.endswith('x')
v6_1 = test.startswith('x')
print(v6)
print(v6_1)
###寻找子序列(可指定区间),找到第一个并返回位置,若没有返回-1
v7 = test.find('ex',1,4)
###若找不到会报错
v17_1 = test.index('ex',1,4)
print(v7,v17_1)
##关键词传值(格式化占位符)
test = '给字符串传值{关键词0}{关键词1}'
v8 = test.format(关键词0='--关键词传值',关键词1='--用等号')
v9 = test.format_map({'关键词0':'--传入字典','关键词1':'--字典元素映射'})
print(v8,v9)
##位置顺序传值(格式化占位符)
test = '根据位置传值{0},{1}'
v10 = test.format('第一个位置0','第二个位置1')
print(v10)

###判断字符串中是否只是字母或是数字
test = 'alex123'
v11 = test.isalnum()
print(v11)

###制表符\t
test = 'name\temail\tpassword\nalax\[email protected]\tsdf'
v12 = test.expandtabs(15)
print(v12)

###是否只包含字母
test = 'asdsf'
v = test.isalpha()
print(v)

###判定一个字符串是否是数字
test = ''
v1 = test.isdecimal()
v2 = test.isdigit()
##判断中文数值
test = ''
v3 = test.isnumeric()
print(v1,v2,v3)
###是否包含不可显示的字符
test = 'isprintable\nfalse'
v = test.isprintable()
print(v)

###是否全是空格
test = 'isspace space'
v = test.isspace()
print(v)

###判断是否是标题并且转换为标题
test = 'Return True if the string is a title-cased string, False otherwise.'
v1 = test.istitle()
v2 = test.title()
v3 = test.istitle()
print(v1,v2,v3)

###拼接字符串
test = '用指定字符拼接字符串元素\n'
v1 = ' '.join(test)
v2 =  '_'.join(['拼接','字符串'])
print(v1,v2)

'''
用 指 定 字 符 拼 接 字 符 串 元 素 
 拼接_字符串
'''


###指定宽度-填充
test = '指定宽度-填充'
v1 = test.ljust(20,"#")
v2 = test.rjust(20,"*")
v3 = test.center(20,"@")
v4 = test.zfill(20)
print(v1)
print(v2)
print(v3)
print(v4)
'''
指定宽度-填充#############
*************指定宽度-填充
@@@@@@指定宽度-填充@@@@@@@
0000000000000指定宽度-填充
'''

###是否小写---变成小写
test = 'Return a copy of the string converted to lowercase.'
v1 = test.islower()
v2 = test.lower()
print(v1)
print(v2)
'''
False
return a copy of the string converted to lowercase.
'''
##判断是否全为大写---变成大写,大写换小写-小写换大写
test = 'Return True if the string is an uppercase string, False otherwise.'
v1 = test.isupper()
v2 = test.upper()
v3 = test.swapcase()
print(v1)
print(v2)
print(v3)
'''
False
RETURN TRUE IF THE STRING IS AN UPPERCASE STRING, FALSE OTHERWISE.
rETURN tRUE IF THE STRING IS AN UPPERCASE STRING, fALSE OTHERWISE.
'''

####处理空格 \n \t
test = '  \n 处 理 \t 空 格 \t  去掉指点字符'
v1 = test.rstrip('字符')
v2 = test.lstrip('字符')
v3 = test.strip('字符')
print(v1)
print(v2)
print(v3)
'''
 处 理      空 格      ##处理右边空格\n \t
处 理      空 格     ##处理左边空格\n \t
处 理      空 格      ##处理左右空格(不处理字符串元素间的空格)
'''

###对应关系替换
test = '建立对应关系,maketrains,进行替换字符,translate'
m = test.maketrans('建立对应关系进行替换字符','对应关系建立替换对应字符')
v= test.translate(m)
print(v)
'''
对应关系建立,maketrains,替换对应字符,translate
'''

####字符串分割,
test = '找到第一个指定元素进行分割\n_分成3分'
v1 = test.partition('')
v2 = test.rpartition('')
v3 = test.split('',2)  ###默认全部找到,但没有分割符,指定寻找次数后,从左到右分割指定次数
v4 = test.splitlines(True)  ###只能根据换行符进行分割,是否保留换行符\n(True)
print(v1)
print(v2)
print(v3)
print(v4)
'''
('找到第一个指定元素进行', '分', '割\n_分成3分')
('找到第一个指定元素进行分割\n_分成3', '分', '')
['找到第一个指定元素进行', '割\n_', '成3分']
['找到第一个指定元素进行分割\n', '_分成3分']
'''

猜你喜欢

转载自www.cnblogs.com/liuhuacai/p/11353694.html
今日推荐