Python9-数据类型-day3
程序员文章站
2022-05-06 10:32:34
数据类型转换 # s = '' >Fasle #非空字符串都是True 字符串的索引与切片 切片:顾头不顾尾 ACABCFABCDLSESRFABCDLSESRFACLDCBADBFRSESLDCBAFRSESLDCBA 字符串操作 每个隔开(特殊字符或者数字)的单词的字母大写 居中和填充 tabl ......
数据类型转换
#int----->str s = 1 i = str(s) print(i) #str----->int s = '123' i = int(s) print(i) #int------->bool i = 3 b = bool(i) print(b)
# s = '' ------>fasle
#非空字符串都是true
#s = "0" --------->true
字符串的索引与切片
切片:顾头不顾尾
s = 'abcdlsesrf'
s1 = s[0]
print(s1)
s2 = s[2]
print(s2)
s3 = s[0:3] #顾头不顾定
print(s3)
s4 = s[-1]
print(s4)
s5 = s[0:]
print(s5)
s6 = s[:]
print(s6)
s7 = s[0:5:2]
print(s7)
s8 = s[3::-1]
print(s8)
s9 = s[3::-2]
print(s9)
s10 = s[-1::-1]
print(s10)
s10 = s[::-1]
print(s10)
a
c
abc
f
abcdlsesrf
abcdlsesrf
acl
dcba
db
frsesldcba
frsesldcba
字符串操作
s = 'alexusir' s1 = s.capitalize() #首字母大写 print(s1)
alexusir
s2 = s.upper() #全部字母大写 print(s2)
############ alexusir
s3 = s.lower() #全部字母小写 print(s3)
alexusir
s4 = s.swapcase() #字母大小写翻转 print(s4)
alexusir
每个隔开(特殊字符或者数字)的单词的字母大写
s = 'alex open user' s5 = s.title() print(s5) ########### alex open user
居中和填充
s = 'alexuser' s6 = s.center(20,'#') #居中和填充 print(s6) ######alexuser######
table 键位
s = 'all\tuser' s7 = s.expandtabs() #table键 print(s7) all user
字符长度
s = '123dfjlasjflasjfklasjf地方' s8 = len(s) print(s8) 24
判断字符串中以什么开头和结尾
s = 'alexwusir' s9 = s.startswith('alex') print(s9) true
s = 'alexwusir' s9 = s.startswith('alex') print(s9) s10 = s.startswith('u',5) print(s10) true true