字符串操作练习:星座、凯撒密码、99乘法表
- 实例:输出12个星座符号,以反斜线分隔。
for i in range(12): print (chr(9800+i),end="/")
- 实例:恺撒密码的编码
plaincode=input ("请输入明文:") for p in plaincode: if ord("a")<=ord("p")<=ord("z"): print(chr(ord("a")+(ord(p)-ord("a")+3)%26),end="") else: print(p,end="")
- 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
a = input('name:') print('the name what you entered: {0:^4}'.format(a))
- 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
print("中华人民共和国国内生产总值(GDP):{0:^-10,.2f}亿元".format(689136.89))
- 实例:打出99乘法表
for i in range(1,10): for j in range(1,i+1): print(j,"*",i,"=",i*j," ",end='') print(end='\n')
6.实例: 下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
s='''we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year good tidings we bring to you and your kin good tidings for christmas and a happy new year we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year and have yourself a merry little christmas, have yourself a merry little christmas night Jingle Bells (The One Horse Open Sleigh) Lyrics:James Lord Pierpont Music:James Lord Pierpont Dashing through the snow, In a one horse open sleigh, Over the fields we go, Laughing all the way; Bells on bobtails ring, Making spiritis bright, What fun it is to ride and sing a sleighing song to night. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! A day or two ago I thought I'd take a ride, And soon Miss Fannie Bright, Was seated by my side; THe horse was lean and lank, Misfortune seemed his lot, He got into a drifted bank, And we, we got upsot. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Now the ground is white, Go it while you're young, Take the girls tonight, And sing this sleighing song. Just get a bobtailed bay, Two-forty for this spead, Then hitch him to an open sleigh, And crack! You'll take the lead. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh!''' print('the 出现的次数:',s.count('the')) s=s.replace(',',' ') s=s.replace('.',' ') s=s.replace('?',' ') s=s.replace('!',' ') s=s.lower() print(s)