• 字符串练习


    一、字符串练习:

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

    取得校园新闻的编号

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

    2.https://docs.python.org/3/library/turtle.html

    产生python文档的网址

    addr1 = 'https://docs.python.org/3/library/'
    addr2 = '.html'
    b = 'turtle'
    print(addr1 + b + addr2)

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

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

    addr1 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'
    for i in range(1,231):
        print(addr1)

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

    (1).用函数得到校园新闻编号

    addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html'
    print(addr.rstrip('.html')[-9:])

    (2).用函数统计一歌词中单词出现的次数

    song = '''I've heard there was a secret chord
    That David played, and it pleased the Lord
    But you don't really care for music, do you?
    It goes like this
    The fourth, the fifth
    The minor fall, the major lift
    The baffled king composing Hallelujah
    
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    
    Your faith was strong but you needed proof
    You saw her bathing on the roof
    Her beauty in the moonlight overthrew you
    She tied you to a kitchen chair
    She broke your throne, she cut your hair
    And from your lips she drew the Hallelujah
    
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    maybe I've been here before
    I know this room, I've walked this floor
    I used to live alone before I knew you.
    I've seen your flag on the marble arch
    Love is not a victory march
    It's a cold and it's a broken Hallelujah
    
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    
    There was a time when you let me know
    What's really going on below
    But now you never show it to me, do you?
    And remember when I moved in you
    The holy dove was moving too
    And every breath we drew was Hallelujah
    
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    
    Maybe there’s a God above
    But all I’ve ever learned from love
    Was how to shoot at someone who outdrew you
    It’s not a cry you can hear at night
    It’s not somebody who has seen the light
    It’s a cold and it’s a broken Hallelujah
    
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah
    Hallelujah, Hallelujah'''
    print(song.count('Hallelujah'))

    (3).将字符串分解成一个个的单词。

    b = 'I am happy!'
    print(b.split())

    二、.组合数据类型练习

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

    str = 'tuple'
    for i in str:
        print(i)
    a = ['l','i','s','t']
    for i in a:
        print(i)
    b = ('t','u','p','l','e')
    for i in b:
        print(i)
    c = {
        'd':1,'i':2,'c':3,'t':4
        }
    for kv in c.items():
        print(kv)
    s = set(['s','e','t'])
    for i in s:
        print(i)

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

    字符串:不能修改的字符序列。除了不能修改,可把字符串当成列表一样处理。

    列表:列表是可变对象,它支持在原处修改的操作,也可以通过指定的索引和分片获取元素。区别于元组,可动态增加,删除,更新。

    元组:元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作,元组和列表是类似的, 只是元组不可修改内容。

     字典:字典最大的价值是查询,通过键,查找值,字典完全区别于列表和元组,因为字典使用hash表来存储, 所以只有能够进行hash运算的才能作为键值。

    集合:集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

  • 相关阅读:
    jQuery 教程
    单例设计模式getInstance()
    JS和JQuery总结
    Anchor 对象
    static关键字
    HTML、html
    HTML DOM
    HTML DOM 实例Document 对象
    水池进水与放水问题:有一个水池,水池的容量是固定 的500L,一边为进水口,一边为出水口.........(多线程应用)
    迷你DVD管理器(Java版)
  • 原文地址:https://www.cnblogs.com/abcdcd/p/8618584.html
Copyright © 2020-2023  润新知