zoukankan      html  css  js  c++  java
  • 字符串练习

    字符串练习:

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

    取得校园新闻的编号

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

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

    产生python文档的网址

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

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

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

    addr1 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    addr2 = '.html'
    for i in range(2,7):
        adrr = addr1 + str(i) +addr2
        print(adrr)

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

    用函数得到校园新闻编号

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

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

    longstr = '''Big Big World
      Emilia
    I'm a big big girl
      In a big big world
      It's not a big big thing if you leave me
      But I do do feel
      that I too too will miss you much
      Miss you much.
    I can see the first leaf falling
      It's all yellow and nice
      It's so very cold outside
      Like the way I'm feeling inside
      I'm a big big girl
      In a big big world
      It's not a big big thing if you leave me
      But I do do feel
      that I too too will miss you much
      Miss you much.
    Outside it's now raining
      And tears are falling from my eyes
      Why did it have to happen
      Why did it all have to end
      I'm a big big girl
      In a big big world
      It's not a big big thing if you leave me
      But I do do feel
      that I too too will miss you much
      Miss you much.
    I have your arms around me ooooh like fire
      But when I open my eyes
      You're gone.
    I'm a big big girl
      In a big big world
      It's not a big big thing if you leave me
      But I do do feel
      that I too too will miss you much
      Miss you much.
    I'm a big big girl
      In a big big world
      It's not a big big thing if you leave me
      But I do feel that will miss you much
      Miss you much.
    '''
    print(longstr.count('big'))

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

    str = 'This is a string example wow !'
    print(str.split())
    print(str.split('i'))

    2.组合数据类型练习

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

    #字符串
    s = 'ABCDF'
    print(s)
    for i in s:
        print(i)
    #列表
    l = list('ABCDF')
    print(l)
    for i in l:
        print(i)
    #元组
    t = tuple('ABCDF')
    print(t)
    for i in t:
        print(i)
    #字典
    d = dict(zip('ABCDF', '12345'))
    print(d)
    for i in d:
        print(i)
    #集合
    s = set('ABCDF')
    print(s)
    for i in s:
        print(i)

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

  • 相关阅读:
    python自动生成bean类
    CVPR2021 | SETR: 使用 Transformer 从序列到序列的角度重新思考语义分割
    经典论文系列 | 缩小Anchor-based和Anchor-free检测之间差距的方法:自适应训练样本选择
    单阶段实例分割综述
    CVPR2021提出的一些新数据集汇总
    使用 PyTorch Lightning 将深度学习管道速度提高 10 倍
    C#中使用ref和out传参的方法及区别
    读书笔记《重构 改善既有代码的设计》(第2版本)
    《大话设计模式》等读后感
    OOP、封装、继承、多态,真的懂了吗?
  • 原文地址:https://www.cnblogs.com/stcy520/p/8608850.html
Copyright © 2011-2022 走看看