python基础之字符串常用方法
'''
字符串常用方法
isdigit
join
replace
find
conut
strip
center
split
format
'''
# s = 'Hello World!'
# # 打印输出字符串,总长度50
# # *******************hello world!*******************
# print(s.center(50,'*'))
# 打印输出字符串,Hello World!**************************************
# print(s.ljust(50,'*'))
# # 判断字符串S是否以感叹号结尾,返回bool ,True
# print(s.endswith('!'))
# # 判断字符串S是否以a开头,返回bool,False
# print(s.startswith('a'))
# # 将字符串大小写转换,hELLO wORLD!
# print(s.swapcase())
# # 将字符串转换为小写, hello world!
# print(s.lower())
# # 将字符串转换为大写,HELLO WORLD
# print(s.upper())
# 查找字符串中的值,并返回查找字符串的第一位的index, 2
# 查不到返回-1,可设置查到范围index
# print(s.find('ll'))
# print(s.find('or',0,2))
# 格式化输出字符串,a,b输出结果一致 my name is kongkongkong, kongkongkong want go to sky
# a = 'my name is {name}, {name} want go to {you} '.format(name = 'kongkongkong',you ='sky')
# b = 'my name is {0}, {0} want go to {1} '.format('kongkongkong','sky')
# print(a)
# print(b)
#获取字符串中的索引,找不到报错 ValueError: substring not found
# print(s.index('o',0,9))
# s = 'Hello World'
#判断字符串是否是XXX,返回bool
# print(s.isalnum()) #是否是阿拉伯数字和字符
# print(s.isalpha()) #是否是纯字母,有特殊符号和空格也不行
# print(s.isdecimal()) #是否是整数
# print(s.isdigit()) #是否是整数
# print(s.isidentifier())#是否是一个合法的变量名
# print(s.islower()) #是否是小写字母
# print(s.isnumeric()) #是否是数字
# print(s.isprintable()) #是否能被打印,如在linux下 视频文件 网卡 是二进制文件不能被打印
# print(s.istitle()) #首字母是否都是大写
# nums = ['1','2','3','4','5']
# print('-'.join(nums)) #常用拼接list
# print(s.lower()) #转小写
# print(s.upper()) #转大写
# print(s.strip()) #去出2边的空格 换行 tab
# print(s.lstrip()) #去出左边的空格 换行 tab
# print(s.rstrip()) #去出右边的空格 换行 tab
# list =s.partition('e')# 使用e 为中心 分割字符串,结果为tuple ('H', 'e', 'llo World')
# print(list)
# b = 'hhh2hhh'
# print(b.split('2')) #分割字符串为列表,分割本身消失 ['hhh', 'hhh']
# c = 'hhhh\nhhhh \n qqqq'
# print(c.splitlines()) #以行分割为列表,常用于读取txt文件 ['hhhh', 'hhhh ', ' qqqq']
#
# print(s.replace('o','hhee')) #日常替换 Hellhhee Whheerld
# print(s.rfind('o')) #反向搜索,find 正向搜索