zoukankan      html  css  js  c++  java
  • 字符串类型

    定义方式:' '," ",''' '''.

    按索引取值(正向取+反向取):只能取

    result = 'hello,world'
    print(result[2])
    print(result[-1])

    2.切片(顾头不顾尾)

    result = 'hello,world'
    print(result[6:])
    print(result[:2])
    print(result[0:3])
    print(result[-3: -1])
    print(result[0:6:2])

    3.长度

    result = 'hello,world'
    print(len(result))
    l = [1,2,3,'a','b', [1,2,3]]
    print(len(l))
    t = (1,2,3,4)
    print(len(t))
    result = {
        'a': 1,
        'b': 2,
        'c': 3
    }
    print(len(result))

    4.成员运算(in,not in)

    result = 'hello,world'
    result = '1888这个班好牛X!'
    if 'X牛' in result:
        print('ok')
    else:
        print('不OK')
    if '牛X' not in result:
        print('ok')
    else:
        print('不OK')

    5.移除空白(strip,lstrip,rstrip)

    result = '------LINUX------'
    print(result.rstrip('-')) #strip()不加参数,默认是空格

    6.切分split,rsplit(把一个字符串变成列表)

    result = '192.168.1.250'
    print(result.split('.'))
    result = '192 168 1 250'
    print(result.split()) #split()默认按空格分割,从左往右分割
    print(result.rsplit('.', 1)) #rsplit()从右往左分割
    print(result.split('.', 1))

    7.lower,upper,title(小写,大写,抬头)

    content = 'linux'
    result = content.upper()
    print(result.lower())
    
    name = 'helloworld'
    print(name.title())

    8.startswith endswith(判断字符串开头和结尾)

    name = 'hello world'
    print(name.startswith('o'))
    print(name.endswith('s'))

    9.format(格式化)

    result = 'my name is {} my age is {}'.format('oyy', 18)
    print(result)
    
    result = 'my name is {name} my age is {age}'.format(age = 18, name='oyy')
    print(result)

    10.replace(替换)

    result = 'my name is oyy my age is 18'.replace('my', 'your', 1)
    print(result)

    11.了解(str.find, rfind, index, rindex, count)

    # result = 'mynameisfengzimyageis18'.index('y')
    # result = 'mynameisfengzimyageis18'.find('fengzi')
    # result = 'mynameisfengzimyageis18'.count('my')
    # print(result)
  • 相关阅读:
    软件测试面试之剖析面试官
    DVWA之文件包含(File inclusion)
    DVWA之跨站请求伪造(CSRF)
    DVWA之命令注入(command injection)
    DVWA之Brute Force教程
    SQL注入中的整型注入实验
    模拟一次CSRF(跨站请求伪造)例子,适合新手
    安卓中Paint类和Canvas类的方法汇总
    C语言程序设计:现代方法(第2版)第三章全部习题答案
    C语言程序设计:现代方法(第2版)第二章全部习题答案
  • 原文地址:https://www.cnblogs.com/wangxudong/p/10059439.html
Copyright © 2011-2022 走看看