zoukankan      html  css  js  c++  java
  • 5、基本数据类型(str)


    5.1、字符串:

    1、n1 = "lc" n2 = 'root' n3 = """chang""" n4='''tom'''

    2、井号(#)表示单行注释,""" """或''' '''表示多行注释也表示字符串

    3、在paycharm中选中代码按"ctrl + ?"键表示多行单行注释

    4、字符串和子字符串的区别:

    "张"是字符

    "张三丰"是字符串

    "张三"是"张三丰"字符串中的子序列

    5、字符串的特点:

    (1)字符串是不可变数据类型(不可以增加,删除、修改元素)

    (2)字符串元素是有序排列的

    (3)字符串元素可以使用for循环进行遍历


    5.2、字符串运算:

    1、加法:

    n1 = "a"

    #赋值

    n2 = "b"

    n4 = "c"

    n3 = n1 + n2 + n4

    # "abc"

    #字符串一旦创建,不可修改

    # 一旦修改或者拼接,都会造成重新生成字符串


    2、乘法:

    n1 = "a"

    n3 = n1 * 3

    #aaa


    5.3、字符串方法:

    1、首字母大写,其余的都小写:

    test = "lC"

    v = test.capitalize()

    print(v)


    2、所有变小写:

    test = "lc"

    v1 = test.casefold()

    print(v1)

    #casefold更厉害,很多未知的对相应变小写

    v2 = test.lower()

    print(v2)


    3、设置宽度,并将内容居中:

    # 20 代指总长度

    # * 空白未知填充,一个字符,可有可无

    v = test.center(20,"中")

    print(v)


    test = "lc"

    v = test.ljust(20,"*")

    print(v)


    test = "lc"

    v = test.rjust(20,"*")

    print(v)


    4、去字符串中寻找,寻找子序列的出现次数:

    test = "lclcliulc"

    v = test.count('lc')

    print(v)


    test = "liuchangliuchang"

    v = test.count('iu',5,6)

    print(v)

    #取m<=下标<M内的子序列出现的次数,下标从0开始;


    5、以什么什么结尾,以什么什么开始:

    test = "liuchag"

    v = test.endswith('l')

    v = test.startswith('g')

    print(v)


    6、expandtabs,从左往右说20个字符,字符不到20又遇到了" "用空格补齐到20:

    test = "username email password lc1 lc1@qq.com 123"

    v = test.expandtabs(20)

    print(v)


    7、从开始往后找,找到第一个子序列后,返回子序列第一个字符所在的下标:

    test = "liuchag"

    v = test.find('iu')

    print(v)

    #未找到 -1


    8、格式化,将一个字符串中的占位符替换为指定的值:

    test = 'i am {name}, age {a}'

    print(test)

    v = test.format(name='liuchang',a=23)

    print(v)


    9、字符串中是否只包含字母和数字:

    test = "123"

    v = test.isalnum()

    print(v)

    10、是否是字母,汉字:

    test = "as2df"

    v = test.isalpha()

    print(v)


    11、当前输入是否是数字:

    test = "1"

    v1 = test.isdecimal()

    #最常用,判断1、2、3

    v2 = test.isdigit()

    #可以判断②

    v3 = test.isnumeric()

    #可以判断中文的“二”

    print(v1,v2,v3)


    12、是否存在不可显示的字符:

    # 制表符

    # 换行

    test = "oiuas dfkj"

    v = test.isprintable()

    print(v)


    13、判断是否全部是空格:

    test = ""

    v = test.isspace()

    print(v)


    14、判断是否是标题:

    test = "Liu Chang"

    v1 = test.istitle()

    print(v1)


    v2 = test.title()

    print(v2)

    #将字符转化为标题

    v3 = v2.istitle()

    print(v3)


    15、将字符串中的每一个元素按照指定分隔符进行拼接:

    test = "liuchang"

    print(test)

    #t = ' '

    v = "_".join(test)

    print(v)


    16、判断是否全部是大小写 和 转换为大小写:

    test = "Liuchag"

    v1 = test.islower()

    #判断字符串是否全为小写

    v2 = test.lower()

    #将字符串全部转为小写

    #v1 = test.isupper()

    #判断字符串是否全为大写

    #v2 = test.upper()

    #将字符串全部转为大写

    print(v1, v2)


    17、移除指定字符串:

    test = " liu "

    v=test.lstrip()

    #移除字符串最左边的空格

    #v=test.rstrip()

    #移除字符串最右边的空格

    #v=test.strip()

    #移除字符串两边的空格,如果两边有 参数也能去除

    print(v)


    test2 = "liu"

    v1=test.rstrip("u")

    #移除字符串最右边u

    print(v1)


    20、分割为三部分(包含指定的字符):

    test = "12s12s12s12"

    v = test.partition('s')

    print(v)

    #从最左边第一个指定字符分

    v = test.rpartition('s')

    print(v)

    #从最右边第一个指定字符分


    21、分割为指定个数(去除指定的字符):

    test = "12s12s12s12"

    v = test.split('s',2)

    print(v)

    #从字符串最左边分割;

    v=test.rsplit('s',2)

    print(v)

    #从字符串最右边分割

    v=test.rsplit('s')

    print(v)

    #将字符串中所有指定的字符串进行分割


    22、分割,只能根据,true,false:是否保留换行

    test = "asdfadfasdf asdfasdf adfasdf"

    v = test.splitlines(False)

    print(v)


    23、以xxx开头,以xx结尾:

    test = "backend1.1.1.1"

    v = test.startswith('a')

    print(v)

    test.endswith('a)


    24、大小写转换,大写转成小写,小写转成大写:

    test = "Liu"

    v = test.swapcase()

    print(v)


    25、字母,数字,下划线:标识符 def class:

    a = "def"

    v = a.isidentifier()

    print(v)

    #判断某个字符串是否是变量


    26、将指定字符串替换为指定字符串:

    test = "liuchang"

    v = test.replace("iu",'ui')

    print(v)

    #将字符串中所有指定的字符替换成指定的字符

    v = test.replace("iu",'ui',1)

    print(v)

    #将指定字符串替换为指定字符串,下标从第几位字符串开始


    27、索引,下标,获取字符串中的某一个字符:

    test = "妹子有种冲我来"

    v = test[3]

    print(v)


    28、切片:

    test = "妹子有种冲我来"

    v = test[0:-1]

    print(v)

    #从字符串 0<=下标<下标结束,-1表示从右往左第1位数;


    29、获取长度:

    #Python3: len获取当前字符串中由几个字符组成

    v = len(test)

    print(v)


    30、小结,7个基本常用字符串转换方法:

    replace/find/join/strip/startswith/split/upper/lower/format


    31、字符串格式化:

    msg='i am %s my hobby is %d' % ("lc",18)

    print(msg)

    #%s表示格式化字符串,字符串可以接收一切数据类型

    #%d表示格式化数字

    #%+60s表示字符串之间的的间隔是多


    tpl = "percent %.2f" % 99.97623

    print(tpl)

    #打印浮点数


    tpl = 'percent %.2f %%' % 99.97623

    print(tpl)

    #打印百分比



    tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}

    print(tpl)

    #传入字典


    print('root','x','0','0',sep=':')

    #将字符串以什么字符分离


    tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)

    #使用format格式








  • 相关阅读:
    PLAYBOOK 命令统计资源利用率 输出本地文件 flask展示
    如何对PFX证书转换成PEM格式证书
    Openshift学习
    Playbook handlers使用
    网络空间安全基础篇(2)----wireshark
    网络空间安全基础篇(1)----nmap的使用
    opencv——图像掩码操作
    opencv图像阈值操作
    在图片上绘制图形和添加文字
    使用opencv去操作树莓派摄像头保存图片和视频
  • 原文地址:https://www.cnblogs.com/LiuChang-blog/p/12316750.html
Copyright © 2011-2022 走看看