首先定义两个变量 ,方便一会改来改去
list1 = ['a','b','c']
name='helloworld'
索引取值
print('hello_fuka!'[0])
print('hello_fuka!'[-1])
切片
print("hello_fuka!"[0:5:]) #单独提取出hello来 hello
print("hello_fuka!"[-5:-1]) #提取fuka
print('hello_fuka!'[-5:-1:])
join(List)把列表中的元素用引号中的内容拼起来
print(''.join(list1))
判断是否以```开始
print('hello world'.startswith('he'))
判断是否以```结束
print('hello world'.endswith('dd')) #False
find和index
rint('asdfasdf'.find('a')) #在字符串中寻找索引 找不到则返回-1
print('abcdeba'.rfind('b')) #
print(name.index('o')) #在字符串中寻找索引 找不到则报错 4
print(name.rindex('o')) #rindex 从右侧开始找 返回6
移除两端的字符 默认移除空格
name =" "+name+" "
print(name)
print(name.strip(' hd')) #elloworl 移除了两端的空格 h和d
name = name.strip()
字符串拆分 返回值是个列表
name = 'a b c d e'
print(type(name.split(' '))) #返回一个列表
print(name.split(' '))
判断字符串是否为数字
print("123是数字吗",'123'.isdigit())
返回大写(小写)的字母
name ='hello world'
name = name.upper()
print(name) #HELLO WORLD
name = name.lower()
print(name)#HELLO WORLD
首字母大写
print('hello world'.capitalize()) #Hello world
所有首字母大写
print('hello world'.title()) #Hello World
替换
print('hello world'.replace('hello','你好'))
统计字符串中出现过几次字符
print('123123abc吴吴吴'.count('吴')) #3
简单的字符串拼接
print('123'+'abc')
统计字符串长度
print('asdfasdfasdfasdfwqerqwer'.__len__()) #24
print(len('asdfasdfasdfasdfwqerqwer')) #24
成员 in |not in
name = 'helloworld'
print('hello'in name) #True
print("good!" not in name) #True not in 不在
文字填充特效
print('zhe'.center(50,'*'))
print('zhe'.ljust(50,"@"))
print('zhe'.zfill(50))
控制制表符的长度
print('a\tb'.expandtabs(9))
大小写转换
print('fasdf EfasdfFF'.swapcase())
# 4.存储一个值or多个值:一个值
# 5.有序or无序:有序
# 6.可变or不可变:不可变