字符串常用函数
find 检测字符串是否包含指定字符,如果是,返回索引
否则返回-1
index 检测字符串是否包含指定字符,如果是返回索引
否则报错
find与index区别
find()只能用于字符串str中,
index可以运用于所有的有序容器中(即字符串str,元组tuple, 列表list)
count 返回字符串在字符串中指定范围内索引出现的次数
replace 将字符串中的str1指定转换为str2,可以指定最大转换次数
# find
str = 'Because of young,with a total want of good in the future'
print(str.find('total')) # 返回的是该字符串的位置
print(str.find('totao')) # m没有返回-1
#index
str = 'Because of young,with a total want of good in the future'
print(str.index('total'))#返回的是该字符串的位置
print(str.index('totao'))#m没有则报错
#count
print(str.count('of'))
#也可以指定索引范围,这里从1-20
print(str.count('of',1,20))
#这里从10-结束
print(str.count('of',10,len(str)))
#replace
print(str.replace('of','in',1))
int()整形
float()浮点型(如52.3E4就是523000.0)
bool()布尔值 真和假,0与1
(空的字符串,列表,字典,元组以及值0都返回false)
逻辑运算符
or 布尔或
and 布尔与
not 布尔非
按位运算符
转换成2进制运算,了解,暂时没看
三元运算符
result = 值1 if 条件 else 值2
如果条件为真:结果为1,反之,为2
例子: a,b=1,2
c=a if a>b else 0
print(a,b,c)
1 2 0