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

    字符串练习:

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

    取得校园新闻的编号

    >>> s="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    >>> s[45:54]
    '1027/8443'

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

    产生python文档的网址

    >>> s1="http://docs.python.org/3/library/"
    >>> s2="turtle.html"
    >>> s=s1+s2
    >>> print(s)
    http://docs.python.org/3/library/turtle.html
    >>>

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

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

    for i in range(2,231):

    "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html".format(i)

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

    用函数得到校园新闻编号

    方法一:

    str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"

    >>> str.replace('_','.').split('.')[3]

     '1027/8443'

    方法二:

    >>> str = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1027/8443.html'

    >>> print(str.rstrip('.html')[-9:])
    1027/8443

    方法三:

    >>> str = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1027/8443.html'

    >>> print(str.rstrip('.html').split('_')[1])
    1027/8443

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

    >>> mus="""
    I'm gonna stand right here
    I'm not gonna lose
    But when the sky falls down
    You'll find me next to you
    I'm gonna stand right here
    And wait for you voice
    But when the worlds
    You know we got no choice, hey
    When you hear the sound
    Of the world of war
    You know that you can see is a big red sky
    When you know you can't make it on your own
    I will be your soldier
    When you falling down and your on the fence
    I will be your shield that will keep you save
    When you know you can't make it on your own
    I will be your soldier
    When you hear the sound
    Of the world of war
    You know that you can see is a big red sky
    When you know you can't make it on your own
    I will be your soldier
    When you falling down and your on the fence
    I will be your shield that will keep you save
    When you know you can't make it on your own
    I will be your soldier
    I will be your soldier
    I will be your soldier"""


    >>> mus.count('soldier')
    6

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

    >>> str="http://news.gzcc.cn/html/xiaoyuanxinwen/9.html"


    >>> str.split('/')
    ['http:', '', 'news.gzcc.cn', 'html', 'xiaoyuanxinwen', '9.html']


    >>> str.split('/',1)
    ['http:', '/news.gzcc.cn/html/xiaoyuanxinwen/9.html']
    >>>

    2.组合数据类型练习

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

    字符串:

    定义:

    >>> str='spring'

    遍历:
    >>> for i in str : print(i)

    s
    p
    r
    i
    n
    g

    列表:

    定义:

    >>> ls=list('5468')
    >>> ls
    ['5', '4', '6', '8']

    遍历:
    >>> for i in ls: print(i)

    5
    4
    6
    8
    >>>

    元组:

    定义:

    >>> tup=tuple('Nacy')
    >>> tup
    ('N', 'a', 'c', 'y')

    遍历:
    >>> for i in tup :print(i)

    N
    a
    c
    y
    >>>

    词典:

    定义方法一:>>> dic1={}
    >>> dic1['Michael']=80
    >>> dic1['Nacy']=98
    >>> dic1['Jake']=77
    >>> dic1
    {'Michael': 80, 'Nacy': 98, 'Jake': 77}
    >>>

    定义方法二:

    >>> dic=dict(zip('tutle','48574888'))
    >>> dic
    {'t': '5', 'u': '8', 'l': '7', 'e': '4'}

    遍历:

    >>> for i in dic : print(i)

    t
    u
    l
    e

    集合:

    定义:

    >>> s=set('Nacy')
    >>> s
    {'y', 'a', 'c', 'N'}

    遍历:
    >>> for i in s:print(i)

    y
    a
    c
    N
    >>>

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

     列表和元组的数据都是有序列的;词典和集合的数都是无序的;列表用[]括号;元组用()括号;词典和集合都是用{}括号;遍历集合和词典时只会显示键(key)。

  • 相关阅读:
    爱加密亮相第十八届软博会,移动App安全引关注
    android 实现自己定义状态栏通知(Status Notification)
    Android中View绘制流程以及invalidate()等相关方法分析
    设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
    目标检測的图像特征提取之(一)HOG特征
    Vbox 未指定XXX网络名称 找不到网卡问题
    NetBeans工具学习之道:NetBeans IDE Java 高速新手教程
    NETSH WINSOCK RESET这条命令的含义和作用?
    红帽/CentOS ext4无法格式化大分区 补充ext4格式化方式
    android之PackageManager简单介绍
  • 原文地址:https://www.cnblogs.com/hhmk/p/8608245.html
Copyright © 2011-2022 走看看