美文网首页Python四期爬虫作业
【Python爬虫】字符串索引切片

【Python爬虫】字符串索引切片

作者: 徐_c90e | 来源:发表于2017-08-19 14:33 被阅读35次

    # 1.理解索引这个会在之后经常用到
    #
    # 2.定义字符串、例如:str1 = 'http://www.jianshu.com/u/a987b338c373'字符串内容为自己的首页连接,输出自己的简书id(u/之后的内容--a987b338c373)
    str1 = 'http://www.jianshu.com/u/a987b338c373'
    print(str1[str1.find('/u/')+len('/u/'):])
    # 3.设s = "abcdefg", 则下列值为
    # s[3]                s[2:4]
    # 'd' 'cd'
    # s[:5]               s[3:]
    # 'abcde' 'defg'
    # s[::-1]             s[::2]
    # 'gfedcba' 'aceg'


    s='abcdefg'
    print(s[3],s[2:4])
    print(s[:5] , s[3:])
    print(s[::-1] , s[::2])

    # 4.定义列表:list1 = [1,2,3,4,5,6,7],则下列值为
    # list1[3]            list1[2:4]
    # 4 [3,4]
    list1=[1,2,3,4,5,6,7]
    print(list1[3],list1[2:4])

    # list1[:5]           list1[3:]
    # [1,2,3,4,5] [4,5,6,7]
    print(list1[:5],list1[3:])

    # list1[::-1]         list1[::2]
    # [7,6,5,4,3,2,1] [1,3,5,7]
    print(list1[::-1],list1[::2])

    # 5.定义元组:touple1 = (1,2,3,4,5,6,7),则下列值为
    # touple1[3]          touple1[2:4]
    # 4 (3,4)
    touple1 = (1,2,3,4,5,6,7)
    print(touple1[3],touple1[2:4])
    # touple1[:5]         touple1[3:]
    # (1,2,3,4,5) (4,5,6,7)
    print(touple1[:5],touple1[3:])
    # touple1[::-1]       touple1[::2]
    # (7,6,5,4,3,2,1) (1,3,5,7)
    print(touple1[::-1],touple1[::2])
    # 6.对之前学习过得集中基本类型及其方法进行复习,重中之重理解索引和切片

    相关文章

      网友评论

        本文标题:【Python爬虫】字符串索引切片

        本文链接:https://www.haomeiwen.com/subject/zrjzrxtx.html