几种数据类型的常用API

  • 字符串 
#==========join===========在字符串前后插入字符
a = 'hello'
b = a.join('abc')
print(b)
#ahellobhelloc

#==========split==========将给定的字符作为切割点,第二个参数表示切割的次数,返回一个切割后的字符串的列表
a = 'heellohe'
b = a.split('e',3)
print(b)
#['h', '', 'lloh', '']

#==========find==========返回待查找的字符所在的第一个位置的索引,未找到就返回-1
# a = 'heellohe'
# b = a.find('e', 3, 8)
# print(b)
#7

#==========strip==========①不传参数:去掉字符串两端的空格②传参数,原字符串两端没有空格:去掉原字符串中传入的参数(必须两端字符)
a = 'heelloheeff'
b = a.strip('f')
print(b)
#heellohee

#==========upper==========将字符串中全部变成大写字母
a = 'FjihrDDDkkkk'
b = a.upper()
print(b)
#FJIHRDDDKKKK

#==========lower==========将字符串中全部变成小写字母
a = 'FjihrDDDkkkk'
b = a.lower()
print(b)
#fjihrdddkkkk

#==========replace==========用新字符替代就旧字符(区分大小写)
a = 'FjihrKKDDDkkkk'
b = a.replace('k', '')
print(b)
#FjihrKKDDD哈哈哈哈

#==========索引==========
test = 'hello'
print(test[2])
#l

#==========切片==========
test = 'hello'
v = test[0:3]   #索引为-1表示最后一个字符,切片时左闭右开[)
print(v)
#hel

#==========len==========返回字符串的长度
test = '1234567'
length = len(test)
print(length)
#7

猜你喜欢

转载自www.cnblogs.com/SakuraYuanYuan/p/10224385.html