博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串练习
阅读量:5809 次
发布时间:2019-06-18

本文共 2946 字,大约阅读时间需要 9 分钟。

字符串练习:

http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

取得校园新闻的编号

 

news = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'print(news[-14:-5])

 

docs.python.org/3/library/turtle.html

产生python文档的网址

 

newshead='https://docs.python.org/3/library/'newsend='.html'print(newshead+str('turtle')+newsend)

 

http://news.gzcc.cn/html/xiaoyuanxinwen/4.html

产生校园新闻的一系列新闻页网址

news1='http://news.gzcc.cn/html/xiaoyuanxinwen/'news2='.html'for i in range(10):    news3=news1+str(i)+news2    print(news3)

 

 

练习字符串内建函数:strip,lstrip,rstrip,split,count

用函数得到校园新闻编号

news = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'print(news.rstrip('.html').split('_')[1])

 

用函数统计一歌词中单词出现的次数

song='''I've been reading books of oldThe legends and the mythsAchilles and his goldHercules and his giftsSpiderman's controlAnd Batman with his fistsAnd clearly I don't see myself upon that listShe said where'd you wanna goHow much you wanna riskI'm not looking for somebodyWith some Superhuman giftsSome SuperheroSome fairytale blissJust something I can turn toSomebody I can kissI want something just like thisDoo doo doo doo doo dooDoo doo doo doo dooDoo doo doo doo doo dooOh I want something just like thisDoo doo doo doo doo dooDoo doo doo doo dooDoo doo doo doo doo dooOh I want something just like thisI want something just like thisI've been reading books of oldThe legends and the mythsThe testaments they toldThe moon and its eclipseAnd Superman unrollsA suit before he liftsBut I'm not the kind of person that it fitsShe said where'd you wanna goHow much you wanna riskI'm not looking for somebodyWith some Superhuman giftsSome SuperheroSome fairytale blissJust something I can turn toSomebody I can missI want something just like thisI want something just like thisOh I want something just like thisDoo doo doo doo doo dooDoo doo doo doo dooDoo doo doo doo doo dooOh I want something just like thisDoo doo doo doo doo dooDoo doo doo doo dooDoo doo doo doo doo dooWhere'd you wanna goHow much you wanna riskI'm not looking for somebodyWith some Superhuman giftsSome SuperheroSome fairytale blissJust something I can turn toSomebody I can kissI want something just like thisOh I want something just like thisOh I want something just like thisOh I want something just like this'''print('some在歌曲里一共出现了'+ str(song.count('some')) +'次')

 

将字符串分解成一个个的单词。

str='Whered you wanna goHow much you wanna risk'print(str.split())

 

2.组合数据类型练习

分别定义字符串,列表,元组,字典,集合,并进行遍历。

字符串:

str='hiro'for s in str:    print(s)

列表:

ls=['h','i','r','o']for s in ls:    print(s)

 

元组:

tuple=('h','i','r','o')for s in tuple:    print(s)

 

字典:

dict = dict(zip('hiro', '5826'))for s in dict:    print(s)for s in dict.keys():     print(s)for s in dict.values():    print(s)for s in dict.items():    print(s)

 

集合:

set = (['hiro'],'hjkl')for i in set:    print(i)

 

总结列表,元组,字典,集合的联系与区别。

联系:

元组可以包含列表,列表衣可以包含元组。

他们能够相互转化。

区别:

元组和列表是有序列的,字典和集合是无序列。

转载于:https://www.cnblogs.com/a565810497/p/8613192.html

你可能感兴趣的文章
Spark之SQL解析(源码阅读十)
查看>>
Android图片添加水印图片并把图片保存到文件存储
查看>>
C#字符串的不变性
查看>>
前端路由简介以及vue-router实现原理
查看>>
比特币系统采用的公钥密码学方案和ECDSA签名算法介绍——第二部分:代码实现(C语言)...
查看>>
分享15款很实用的 Sass 和 Compass 工具
查看>>
AMD优势: 与众不同 选择丰富
查看>>
玩转高性能超猛防火墙nf-HiPAC
查看>>
简单按日期查询mysql某张表中的记录数
查看>>
自动化部署之jenkins发布PHP项目
查看>>
C/C++编程可用的Linux自带工具
查看>>
如何判断webview是不是滑到底部
查看>>
海贼王十大悲催人物
查看>>
org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 搞定!
查看>>
热点热词新闻资讯API开放接口(永久免费开放)
查看>>
8.1_Linux习题和作业
查看>>
11.排序算法_6_归并排序
查看>>
Redis redis-cli 命令列表
查看>>
.NET框架设计—常被忽视的框架设计技巧
查看>>
BigDecimal 舍入模式(Rounding mode)介绍
查看>>