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

    a=‘hello world’

    #底层做二进制用:

    a.zfill(40)   #不够四十位从左侧以‘0’填充
    '00000000000000000000000000000hello world'

    大小写反转:

    print(a.swapcase())

    首字母大写:

    print(a.capitalize())

    把混合大小写的变成纯小写:

    print(a.casefold())

    center:让str有20个字符长 不够的用‘×’填充

    print(a.center(20,‘×’))

    #左右居中:

     a.ljust(50,'-')
    'Alex----------------------------------------------'
     a.rjust(50,'-')
    '----------------------------------------------Alex'

    数数:查看0到3之间有几个‘a’

    print(a.count(‘a’,0,3))

    查是不是以什么结束可以切片查询:

    print(a.endswith(‘a’,0,3))

    制表符:

    a = a b

    print(a.expandtabs(20))

    查索引find和index:

    print(a.find('a',0,2))  #后面可以加开始索引和结束索引  默认左边的

    print(a.rfind(‘a’,0,2))   #后面可以加开始索引和结束索引   它是找最右边的     找不到会返回‘-1’

    #从右边找‘a’

    print(a.rfind('a'))

    index方法:

    print(a.iindex('a',0,2))  #可以跟步长

    #从右边找‘a’

    print(a.rindex('a',0,2))  #可以跟步长   找不到会报错

     查是不是以'a'什么开始:

    print(a.startswith('a'))

    每个字母开头大写:

    print(a.title())

    查看是不是以什么结尾:

    print(a.endswith('a'))

    改小写:

    print(a.lower())

    改大写:

    print(a.upper())

    是不是大写:

    print(isupper())

    是不是小写:

    print(islower())

    是不是空格:

    print(a.isspace())

    是不是小数:

    print(a.isdecimal())

    是不是数字:

    print(a.isdigit())

    print(a.isdecimal())

    print(a,isnumeric() )

    #判断是不是可以被打印:

    print(a.isprintable())

    是不是字母:

    print(a.isalpha())

    是不是字母数字混合:

    print(a.isalnum())

    去空格也可以去其他符号:

    print(a.strip())

    str变列表:

    print(a.split('o',1))  #默认左边第一个‘o’   #如果不写指定换的次数就全部被替换没有了‘o’或者空格分开

    #可以从右边以某个字符分隔:

    print(a.rsplit(‘l’,1))  #右边第一个‘o’    #如果不写指定换的次数就全部被替换没有了‘o’或者空格分开

    a = 'a b Alex'

    print(a.splitlines())    #是以换行符为截断换成列表

     

    列表变str:

    list = " ".join(a)

    print(slist)

    替换后面可以跟数字以表示替换次数 :

    print(a.replace('A','b',1))   #如果里面有多个‘A’默认全部替换  以可以后面加替换次数

    从左边以某个字符分隔:

    print(a.partition('l'))

    从右边以某个字符分隔:

    print(a.rpartition('l'))

    #判断是不是合法的变量名字:

     '6stedo'.isidentifier()
    False

    #加密和解密:

    设置变量

    a = 'Alex'

    加密

    str_in = 'abcdefg'


    str_out = '@!#$%^&'


    str.maketrans(str_in,str_out)


    {97: 64, 98: 33, 99: 35, 100: 36, 101: 37, 102: 94, 103: 38}

    密码本赋值:
    table = str.maketrans(str_in,str_out)

    展示密码本:
    table
    {97: 64, 98: 33, 99: 35, 100: 36, 101: 37, 102: 94, 103: 38}

    解密:
    a.translate(table)


    'Al%x'

    #partition:

    a = 'Alex'

    a.partition(‘e’)

    ('Al', 'e', 'x')  #   以‘e’为结尾分开两段

    python2中有数据类型分别是str和unicode和bytes

    python3有str,bytes

  • 相关阅读:
    不要做优化了!让编译器去干吧!
    arp欺骗进行流量截获-2
    arp欺骗进行流量截获-1
    Python3网络爬虫(1):利用urllib进行简单的网页抓取
    python3.5opencv3图像文字标注
    python3.5连接MySQL
    MySQL安装
    Matplotlib python 基本用法
    servlet表单中get和post方法的区别
    任意目录下启动tomcat
  • 原文地址:https://www.cnblogs.com/yuexijun/p/9783335.html
Copyright © 2011-2022 走看看