#–author:zhaozhao
#date:2020/07/29
① String的内置用法
st='hello\t kitty {name} is {age}'
st.count('l') # 统计元素个数
st.capitalize() # 使首字母大写
st.center(50,'#') # 居中,用#补齐50个字符
st.endswith('tty3') # 判断是否以某个内容结尾,返回布尔值
st.startswith('he') # 判断是否以某个内容开头
st.expandtabs(tabsize=20) # 指定\t的长度为20个字符
st.find('t') # 找到第一个元素t,并将索引值返回,找不到则为-1
st.format(name='zhaozhao',age=18) # 格式化输出
st.format_map({'name':'zhaozhao','age':18}) # 格式化输出
st.index('t')) # 找不到为错误
print('zx2'.isalnum()) # 判断是否为字母和数字
print('12632178'.isdecimal()) # 判断是否为正整数
print('1269999.uuuu'.isnumeric()) # 判断是否为正整数
print('Abc'.islower()) # 全小写
print('ABC'.isupper()) # 全大写
print('My title'.istitle()) # 是否为标题格式,每个首字母大写
print('My tLtle'.lower()) # 输出字符串的小写形式
print('My tLtle'.upper()) # 输出字符串的大写形式
print('My tLtle'.swapcase()) # 输出字符串的大小写转换
print('My tLtle'.ljust(50,'*')) # 右边加50个*
print('My tLtle'.rjust(50,'*')) # 左边加50个*
print('\tMy tLtle\n'.strip()) # 去掉字符串两边的空格tab键和换行
print('\tMy tLtle\n'.lstrip()) # 去掉字符串左边的空格tab键和换行
print('\tMy tLtle\n'.rstrip()) # 去掉字符串右边的空格tab键和换行
print('My title title'.replace('itle','lesson',1)) # 用lesson替换一次itle
print('My title title'.rfind('t')) # 从右往左找第一个出现的t,起始位为0
print('My title title'.split('i',1)) # 用i分割一次
print('My title title'.title()) # 以标题格式输出
str=input("please:")
# str.count(sub[, start[, end]]) 其中的[]代表可选参数
print(str.count('zx', 0, len(str))) # 找一个字符串在原字符串中出现的次数,start和end可以自定义
print(len(str)) # 获取字符串长度
print(str.find("zx")) # 从左往右检索字符串是否存在子串,若存在返回起始索引(从0开始),如果不存在返回-1
print(str.strip("$")) # 同时删除左右两边的指定字符,若无参数则为删除空格
print(str.replace(" ",'',2)) # 替换空格(old,new)两次
print(str.split(' ')) # 以空格划分,存放于列表中
② +:字符串拼接,*:重复输出
print("zxy"+"fwb") # 将fwb链接到zxy后面
print("aoe"*4) # 将aoe重复输出4次
③ 字符串格式化 拼接 使用占位符,可对具体值进行格式化
num=2
score=320
res="我的编号:%03d 我的成绩:%.1f" %(num,score) # 选取三位整数和小数点后两位小数。我的编号:002 我的成绩:320.0
print("我的编号:%03d 我的成绩:%.1f" %(num,score))
# %c格式化字符集和ascii %s字符串 %d整数 %f小数
# format格式化函数,通过花括号来代替之前的占位符的%,可以接收无限个参数,位置也可以不按顺序,指定下标
print("say:'{1}''{0}'".format('hello','zhanghao'))
# say:'zhanghao''hello'
num=2
score=328.5
print("say:{0:02d} {1:.2f}".format(num,score))
# say:02 328.50