• pythonc成长之旅4


    ---恢复内容开始---

    一、整形“魔法”(理论水平不够,不知道该叫什么,应该是整形数据库重点一种功能吧)

    (1).至少用几位二进制来表示

    age = 5
    
    #1     1
    #2     10
    #3    11
    #4    100
    #5     101    
    
    
    r = age.bit_length()
    print(r)
     

    (2).将字符串转化为数字

    1 # name = "123"
    2 # print(type(name),name)
    3 # v = int(name)
    4 # print(v)

    #输出结果

    <class 'str'> 123
    <class 'int'> 123

    #字符串已经转化为整形


    5 num = "0011" num必须用机器码 6 v = int(num,base=10) #将num转化为几进制 7 print(type(v),v)

    二、字符串函数(魔法)

      1     一、B.capitalize()
      2             Return a copy of B with only its first character capitalized 
      3             (ASCII)and the rest lower-cased.
      4             #字符串首字母大写,对字母有效,对数字无效。
      5 
      6 name = "counrse"
      7 old = "five years"
      8 v = name.capitalize()                输出结果:    Counrse
      9                                                 Five years    
     10 t = old.capitalize()
     11 print(v)
     12 print(t)   
     13 
     14     二、B.casefold()
     15             B.casefold() -> str
     16             Return a version of S suitable for caseless comparisons.
     17             #将字符串中大写字母变成小写字母。
     18 name = "CouNrse"
     19 v = name.casefold()                    输出结果:counrse
     20 print(v)
     21 
     22     三、center(self, width, fillchar=None)
     23             Return S centered in a string of length width. Padding is
     24             done using the specified fill character (default is a space)
     25             #自定义一个长度,将字符串置于中间,其他位置填充你输入的字符。
     26             #如果不输入,系统默认为填充空格。
     27 name = "counrse"
     28 v = name.center(20,'*')                输出结果:******counrse*******    
     29 print(v)
     30     
     31     四、count(self, sub, start=None, end=None)
     32             Return the number of non-overlapping occurrences of substring sub in
     33             string S[start:end].  Optional arguments start and end are
     34             interpreted as in slice notation.
     35             #设置一个开始位置,一个结束位置,然后查找该字符串中的子序列。
     36             #如果不输入,系统默认为从第一个开始,最后一个结束。
     37 name = "counrse"
     38 v = name.count('un', 1, 6)            输出结果:1
     39 print(v)
     40 
     41     五、encode(self, encoding: str = ..., errors: str = ...) -> bytes(编码)
     42     
     43 str = "我从初中就一直喜欢你喜欢你";
     44 str_utf8 = str.encode("UTF-8")
     45 str_gbk = str.encode("GBK")
     46 
     47 print(str)
     48 print("UTF-8 编码:", str_utf8)
     49 print("GBK 编码:", str_gbk)
     50 
     51 print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
     52 print("GBK 解码:", str_gbk.decode('GBK','strict'))
     53         
     54                                     输出结果;    我从初中就一直喜欢你
     55                                                 UTF-8 编码: b'xe5x85xb6xe5xaex9exe6x88x91xe4xbbx8exe5x88x9dxe4xb8xadxe5xb0xb1xe4xb8x80xe7x9bxb4xe5x96x9cxe6xacxa2xe4xbdxa0'
     56                                                 GBK 编码:  b'xc6xe4xcaxb5xcexd2xb4xd3xb3xf5xd6xd0xbexcdxd2xbbxd6xb1xcfxb2xbbxb6xc4xe3'
     57                                                 UTF-8 解码: 我从初中就一直喜欢你
     58                                                 GBK 解码: 我从初中就一直喜欢你
     59                                                 Process finished with exit code 0
     60 
     61     六、endswith(self, suffix, start=None, end=None)
     62             Return True if S ends with the specified suffix, False otherwise.
     63             With optional start, test S beginning at that position.
     64             With optional end, stop comparing S at that position.
     65             suffix can also be a tuple of strings to try.
     66             #输入一个字母,该函数判断是否以该字母结束,可以设定起始位置和结束位置。输出结果为bool.
     67             #有endswith  则有startswith(),用法与endswith相同。
     68         
     69 name = "counrse"
     70 v = name.endswith('s',1,3)           输出结果;False
     71 print(v)
     72         
     73     七、expandtabs(self, tabsize=8)
     74             Return a copy of S where all tab characters are expanded using spaces.
     75             If tabsize is not given, a tab size of 8 characters is assumed.
     76             #自己去设置字节长度,如果为设置,系统默认为8个字节长度。遇到	补充为剩余字节长度。
     77             #补充:
      	可以用来制作表格。
     78 
     79 name = "counrs123456	789"
     80 v = name.expandtabs()                输出结果:counrs123456    789 19
     81 print(v,len(v))
     82 
     83     八、find(self, sub, start=None, end=None)
     84             Return the lowest index in S where substring sub is found,
     85             such that sub is contained within S[start:end].  Optional
     86             arguments start and end are interpreted as in slice notation.
     87         
     88             '''Return -1 on failure.'''
     89             #查找子序列在该字符串的那个位子,可设置开始和最后的查找位置。
     90 name = "counrse"
     91 v = name.find('n')                  输出结果:3
     92 print(v)
     93             
     94     九、format(self, *args, **kwargs)
     95             Return a formatted version of S, using substitutions from args and kwargs.
     96             The substitutions are identified by braces ('{' and '}').
     97             #格式化,将一个字符串中的占位符替换为指定的值。
     98             #('{' and '}').只能替换这些占位符。如果不指定,即遵循从左到右的顺序。
     99 name = "I like {sky},numbe {a}"
    100 print(name)
    101 v = name.format(sky='sex',a=0 )        输出结果;    I like {sky},numbe {a}
    102                                                 I like sex,numbe 0
    103 print(v)
    104 
    105             
    106 
    107 
    108     十、format_map(self, mapping)
    109             Return a formatted version of S, using substitutions from mapping.
    110             The substitutions are identified by braces ('{' and '}').
    111             #格式化,传入的值如{"hero":'le'}
    112 name = "counrse is {hero}"
    113 v = name.format_map({"hero":'le'})    输出结果:    counrse is le
    114 
    115 print(v)
    116     
    117     十一、index(self, sub, start=None, end=None)
    118             Return the lowest index in S where substring sub is found, 
    119             such that sub is contained within S[start:end].  Optional
    120             arguments start and end are interpreted as in slice notation.
    121         
    122             #Raises ValueError when the substring is not found.
    123             #同found,找不到会报错,所以一般情况用found。
    124             
    125     十二、isalnum(self)
    126             Return True if all characters in S are alphanumeric
    127             and there is at least one character in S, False otherwise.
    128             #该字符串全是字母数字返回ture,有其他的返回false.
    129 name = "counrse-=-"
    130 v = name.isalnum()                    输出结果:False
    131 print(v)
    132 
    133     
    134 name = "counrse"
    135 v = name.isalnum()                    输出结果:True
    136 print(v)
    137     
    138     十三、isalpha(self)
    139             Return True if all characters in B are alphabetic
    140             and there is at least one character in B, False otherwise.
    141             #该字符串全是字母返回ture,否则返回false.
    142 name = "cournse123=-"
    143 v = name.isalpha()                    输出结果:False
    144 print(v)
    145     
    146 name = "cournse123=-"
    147 v = name.isalpha()                    输出结果:True
    148 print(v)
    149     
    150     十四、isdigit(self)
    151             Return True if all characters in B are digits
    152             and there is at least one character in B, False otherwise.
    153             #数字返回Ture,否则返回Flase
    154             
    155     十五、islower(self)
    156             Return True if all cased characters in B are lowercase and there is
    157             at least one cased character in B, False otherwise.
    158             #判断是否全是小写。lower将字符串全部小写化。
    159             #判断全部是否是大写以及大写化。isupper()/upper().
    160     
    161     十六、isdecimal(self)、isdigit(self)、isnumeric(self)
    162             #都是判断字符串是否是数字
    163             #第一种支持数字
    164             #第二种支持数字、特殊数字,如②
    165             #第三种支持数字、特殊数字、中文数字、如二
    166             
    167     十七、isprintable(self)
    168     
    169             #判断是否存在不可显示的字符,如	,
    .
    170     
    171     十八、isspace(self)
    172             
    173             #判断是否全为空格。
    174             
    175     十九、istitle(self)、title(self)
    176     
    177             #第一个判断是否为英文标题。
    178             #第二个转化为英文标题。
    179 
    180 a = "Return True if all characters in B are whitespace"
    181 v1 = a.istitle()
    182 v2 = a.title()                        输出结果:    False
    183                                                 Return True If All Characters In B Are Whitespace
    184 print(v1)
    185 print(v2)
    186     
    187         ***二十、join(self, *args, **kwargs)
    188                 #进行指定字符串拼接,每一个字符都与你所指定的字符拼接。
    189         
    190 title = "快过来被我亲"
    191 print(title)
    192 t = ' --  '                            输出结果:    快过来被我亲
    193 v = t.join(title)                                快 --  过 --  来 --  被 --  我 --194 print(v)
    195 
    196         二十一、ljust(self, width, fillchar=None)
    197                 rjust(self, width, fillchar=None)
    198                 Return B left justified in a string of length width. Padding is
    199                 done using the specified fill character (default is a space).
    200                 #同center、把填充字符放左边(),第二个把填充字符放在右边。
    201                 
    202         二十二、.isidentifier()
    203                 #判断是否是标识符(数字、下划线、字母)
    204 
    205 name = "_1Cadfkjafhkj"
    206 v = name.isidentifier()                输出结果:True
    207 print(v)
    208     
    209 
    210     
    211 name = "1Cadfkjafhkj"
    212 v = name.isidentifier()                输出结果:False
    213 print(v)
    214         
    215         二十三、lstrip(),rstrip(),strip()
    216                 #分别是处理左边空格、
    、	.和右边空格、
    、	.,以及两边空格、
    、	的。
    217                 #指定内容,便去掉指定内容。*如果你输入的字符串不是原字符串子序列,然而你输入的字符串有
    218                 #原字符串的子序列,则函数自动去匹配。
    219 name = " SDKJHKSHGKJSKGSK"
    220 v = name.rstrip('186GSK')            输出结果: SDKJHKSHGKJ
    221 print(v)
    222                 
    223         二十四、translate(self, *args, **kwargs)、 maketrans(*args, **kwargs)
    224                 #指定对应关系,并替换。
    225                 
    226 name = " SDKJH32544552KSHGKJSKGSK"
    227 m = str.maketrans("HKG", "我爱你")    输出结果: SD爱J我32544552爱S我你爱JS爱你S爱
    228 new_name = name.translate(m)
    229 
    230 print(new_name)
    231 
    232         二十五、partition()、rpartition()、 rsplit()、split()、splitlines()
    233         #以你指定的字符分割,p~和rp~分割出来有你指定的字符,而s~和rs~分割出来没有你指定的字符。
    234         #另外,正则表达式,有你设定如何分割。
    235         #splitlines主要以
    为分割点。
    236         
    237         
    238 name = "cosurses"        
    239 new_name = "cosu
    rse	s"
    240 v = name.partition('s')
    241 v1 = name.rpartition('s')
    242 v2 = name.split('s')
    243 v3 = name.rsplit('s')
    244 v4 = new_name.splitlines()            输出结果:         ('co', 's', 'urses')
    245 print(v)                                            ('cosurse', 's', '')
    246 print(v1)                                            ['co', 'ur', 'e', '']
    247 print(v2)                                            ['co', 'ur', 'e', '']
    248 print(v3)                                            ['cosu', 'rse	s']
    249 print(v4)
    250 
    251 
    252         二十六、swapcase(self)
    253                     Return a copy of B with uppercase ASCII characters converted
    254                     to lowercase ASCII and vice versa.
    255                 #将大写转化为小写,将小写转化为大写。
    256                 
    257 name = "c678679696ouAAAAnre141414s"
    258 v = name.swapcase()                    输出结果:C678679696OUaaaaNRE141414S
    259 print(v)
    260 
    261 
    262 
    263         
    264                 
    265 
    266 
    267                 


  • 相关阅读:
    sizeof与strlen的区别
    面试题46:求1+2+...+n
    opennebula 安装指定参数
    opennebula 开发记录
    virsh 查看hypervisor特性
    opennebula kvm日志
    Cgroup
    opennebula kvm 创建VM oned报错日志
    opennebula kvm 创建虚拟机错误
    golang hello
  • 原文地址:https://www.cnblogs.com/lhai000/p/9368523.html
Copyright © 2020-2023  润新知