数据类型之字符串
①按照索引取值,正向取和反向取,只能取
msg=”hello word”
print(msg[0])
print(msg[-1])
②切片,顾头不顾尾,步长
msg=”hello word”
print(msg[0:5:])
print(msg[:])
print(msg[0::])
print(msg[-1:-5:-1])
③长度len
print(len(msg))
④成员运算 in 和 not in
msg=’yangyuanhu 老师是一个非常虎的老师’
print(‘yangyuanhu’ in msg)
print(‘虎’ not in msg)
print(not ‘虎’ in msg)
⑤移除字符串左右两边的字符strip:默认去空格
pwd=’ 1 23 ’
res=pwd.strip(’ ‘)
print(res)
⑥切分split:针对有规律字符串按照某个字符切成列表
info=’yyhdsb|18|female’
li=info.split(‘|’,1)
print(li)
⑦循环
msg=’hello’
for item in msg:
print(item)
1、strip,lstrip,rstrip
print(‘*egon‘.strip(‘*’))
print(‘*egon‘.lstrip(‘*’))
print(‘*egon‘.rstrip(‘*’))
2、lower,upper
print(‘AAAbbbb’.lower())
print(‘AAAbbbb’.upper())
3、startswith,endswith
print(‘alex is sb’.startswith(‘alex’))
print(‘alex is sb’.endswith(‘sb’))
4、format的三种玩法
print(‘my name is %s my age is %s’ %(‘egon’,18))
print(‘my name is %s my age is %s’ %(18,’egon’))
print(‘my name is {name} my age is {age} ‘.format(age=18,name=’egon’))
了解
print(‘my name is {} my age is {} ‘.format(18,’egon’))
print(‘my name is {0} my age is {1} ‘.format(18,’egon’))
print(‘my name is {1} my age is {0} ‘.format(18,’egon’))
5、split,rsplit
msg=’a:b:c:d:e’
print(msg.split(‘:’,1))
print(msg.rsplit(‘:’,1))
6、join
msg=’a:b:c:d:e’
list1=msg.split(‘:’)
msg1=’:’.join(list1)
print(msg1)
info=’egon:123:male’
list1=info.split(‘:’)
print(list1)
print(‘:’.join(list1))
7、replace
msg=’alex is alex alex is hahahaha’
print(msg.replace(‘alex’,’SB’,1))
8、isdigit
print(‘123’.isdigit()) # 只能判断纯数字的字符串
print(‘12.3’.isdigit())
age_of_db=30
inp_age=input(‘>>>: ‘).strip()
if inp_age.isdigit():
inp_age=int(inp_age)
if inp_age > age_of_db:
print(‘too big’)
elif inp_age < age_of_db:
print(‘too small’)
else:
print(‘you got it’)