2019年12月29日 MRKJ page125 实战+字符串

checi=['T40','T298','Z158']
dizhi=['cj','bj','sh']
atime=['2h','1h','3h']
c1=dict(zip(checi,dizhi))
print(c1)
a1=dict(zip(checi,atime))
print(a1)
print('checi','	','dizhi','	','atime')
for i in checi:
    print(i,'	',c1[i],'	',a1[i])#注意	的用法和效果
cc=input('you buy the checi:')
print('sorry' if cc not in checi else '')
people=input('your name:')
print('you buy zhe'+c1[cc]+','+people+' please take the ticket')

 》》》》

{'T40': 'cj', 'T298': 'bj', 'Z158': 'sh'}
{'T40': '2h', 'T298': '1h', 'Z158': '3h'}
checi dizhi atime
T40 cj 2h
T298 bj 1h
Z158 sh 3h
you buy the checi:T40

your name:SXJ
you buy zhecj,SXJ please take the ticket

字符串也可以同list一样做切片

s='人生苦短,我用python!'
print(s[0:4])
print(s[3])
print(s[3:])
print(s[:8])
print(s[::-1])#倒序

》》》

人生苦短

短,我用python!
人生苦短,我用p
!nohtyp用我,短苦生人

s='人 生 苦 短,我用python!'
a=s.split(',')
b=s.split()
c=s.split(' ',2)
print(a,'
',b,'
',c)

d='*'.join(s) #s是可迭代对象,所以可以是list,可以是元祖,也可以是str
print(d)

name=['a','b','c']
e='@'.join(name)
print(e) #注意第一个没有@的
print('@'+e)

》》》

['人 生 苦 短,我用python!']
['人', '生', '苦', '短,我用python!']
['人', '生', '苦 短,我用python!']
人* *生* *苦* *短*,*我*用*p*y*t*h*o*n*!
a@b@c
@a@b@c

s='人 生 苦 短,我用python!'
f=s.count(' ')
g=s.find(' ')#返回首次出现的位置,不存在则返回-1
print(f,g)
h=' ' in s
print(h)#用in 方法判断字符串内是否存在指定内容
i=s.rfind(' ')#从右边开始寻找
print(i)
j=s.startswith('#')
k=s.endswith('!')
print(j,k)

>>>>

3 1
True
5
False True

原文地址:https://www.cnblogs.com/python1988/p/12116082.html