学习心得_str内置函数_纯代码

首先定义两个变量 ,方便一会改来改去

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不可变:不可变

转载于:https://www.cnblogs.com/blog5434/p/10912137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值