• python学习-day03:整形、字符串常用方法:


    一、数字,int

    1.1;

    a、int(object)转化数字类型; 

    a='100'
    b=int(a)
    b=b+1000
    print(b)
    223 <class 'int'>
    答案

    b、转化二进制;

      v=int(num,base=2) #number以二进制的形式转化为 10进制

    num='0011'
    v=int(num,base=2)
    print(v)
    num='0011'
    v=int(num,base=16)
    print(v)
    num='b'
    v=int(num,base=16)
    print(v)==10
    3、17
    答案

    1.2  bit_length  查看二进制的最小表示位数;*.bit.length()

    a=123
    b=a.bit_length()
    print(b)
    结果:7
    答案

    二、字符串转化方法:

    text="alex"

    2.1、capitalize 首字母转化为大写:

    text="alex"

    v=text.capitalize() 

    print V

    >>>Alex

    2.2、casefold()和lower()所有字母变小写。casefold更加牛逼,很多未知的都可以对应变小写;很多国家的对应字符都可以转化:

    test='alex'
    v1 = test.casefold()
    print(v1)
    v2 = test.lower()
    print(v2)
    >>
    alex
    alex

    2.3、center(self, width, fillchar=None):总长度:fillchar:空白位置填充(只填一个字符 )

    test='alex‘
    v = test.center(20) 一共20个字符 
    print(v)
            alex        
    
    test='alex‘
    v = test.center(20,’中‘)
    print(v)
    
    >>>
    中中中中中中中中alex中中中中中中中中

    2.4、count(self, sub, start=None, end=None):计算指定字符或子序列出现的个数。可以加入:起始和结束位置。

    test = "aLexalexr"
    v = test.count('ex',1,5)
    print(v)
    
    >>>1

    2.5、encode(self, encoding='utf-8', errors='strict')和decode  后面再说!

    2.6、endswith(self, suffix, start=None, end=None)  以什么(字符或者子序列)结尾。计算开始和结束的位置。这样的字符会不一样。

    test = "alex"
    v = test.endswith('ex')
    print(v)
    v = test.startswith('ex')
    print(v)
    
    >>>
    True
    False

    2.7、expandtabs(self, tabsize=8)   断句:制表符

    test = "username	email	password
    laiying	ying@q.com	123
    laiying	ying@q.com	123
    laiying	ying@q.com	123"
    v = test.expandtabs(20)
    print(v)
    >>>>
    username            email               password
    laiying             ying@q.com          123
    laiying             ying@q.com          123
    laiying             ying@q.com          123

    2.8、查找指定序列的位置;find、index

           1、find(self, sub, start=None, end=None)。从开始往后找,获取第一个的位置。后面就不继续找了。可以通过设置起始和结束位置来控制。【start,end)

       2、index(self, sub, start=None, end=None)。找不到则会报错

    test = "alexalex"
    # 未找到 -1
    v = test.find('ex')
    print(v)
    
    >>>2

    2.9、format(self, *args, **kwargs) 格式化,将一个字符串中的占位符替换为指定的值

    test = 'i am {name}, age {a}'
    print(test)
    v = test.format(name='alex',a=19)
    print(v)
    
    >>>>
    i am {name}, age {a}
    i am alex, age 19
    
    test = 'i am {0}, age {1}'
    print(test)
    v = test.format('alex',19)
    print(v)
    
    >>>
    i am {0}, age {1}
    i am alex, age 19

    2.10、isalnum(self)   判断字符串中,是否只包含:字母和数字:

    test = "123ww"
    v = test.isalnum()
    print(v)
    
    >>>
    True

    2.11  isalpha()  判断字符串是否全为字母;

    test = "as2df"
    v = test.isalpha()
    print(v)
    
    >>>>
    False

    2.12   isnumeric()、判断十进制数  isdigit()、可以判断其他格式的数字  isdecimal()可以判断各种范围内的数字

    test = "1"
    v1 = test.isdecimal()
    v2 = test.isdigit()
    v3 = test.isnumeric()
    print(v1,v2,v3)
    >>>>>
    True True True

    test = "" v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print(v1,v2,v3) >>> False True True test = "" v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print(v1,v2,v3) >>>> False False True

    2.13、isdecimal 判断字母是合法标识符号,包括已有标量,重新命名

    test = "def"
    v = test.isidentifier()
    print(v)
    >>>
    True

    2.14 islower()和lower()、isupper()和upper()判断是否全部为大小写和转化大小写

    test = "Alex"
    v1 = test.islower()
    v2 = test.lower()
    print(v1, v2)
    
    v1 = test.isupper()
    v2 = test.upper()
    print(v1,v2)
    
    >>>>
    False alex
    False ALEX

    2.15、isprintable()否存在不可显示的字符。

    #不可显示字符
    # 	   制表符
    # 
       换行
    test = "oiuasdfkj
    "
    v = test.isprintable()
    print(v)
    
    >>>
    False

      

  • 相关阅读:
    转自苦大师:移动测试Appium之API手册
    怨念与发飙
    Asp.Net 2.0新特性
    汉字是最优美的文字
    加入cnblog留念
    Thrift之TProtocol类体系原理及源码详细解析之其他协议类和总结
    Thrift之TProtocol类体系原理及源码详细解析之二进制协议类TBinaryProtocolT(TBinaryProtocol)
    linux内核bug问题排查过程详细报告
    Thrift之TProtocol类体系原理及源码详细解析之紧凑协议类TCompactProtocolT(TCompactProtocol)
    Thrift之TProcess类体系原理及源码详细解析
  • 原文地址:https://www.cnblogs.com/laixiaoyun/p/6111898.html
Copyright © 2020-2023  润新知