zoukankan      html  css  js  c++  java
  • python3基础之“函数(1)”

    1.type:查看当前字符串的类型

     1 c='123'
     2 print(type(c),c)
     3 b= int(c)
     4 print(type(b),b)
     5 
     6 num="01010"
     7 a=int(num,base=16)
     8 print(a)
     9 
    10 >><class 'str'> 123
    11 >><class 'int'> 123

    2.bit_length:查看当前数字的二级制多少位

    1 a=10
    2 v=a.bit_length()
    3 print(v)
    4 
    5 >>4

    3.capitalize:首个字母大写

    1 test="aLse"
    2 v=test.capitalize()
    3 print(v)
    4 
    5 >>Alse

    4.(casefold,lower):所有字母变小写,casefold能使更多未知的对应变小写

    1 test="ASD"
    2 v1=test.casefold()
    3 print(v1)
    4 v2=test.lower()
    5 print(v2)

    5.swapcase:大小写转换

    1 a="ABC"
    2 c=a.swapcase()
    3 print(v)
    4 
    5 >>abc
    1 test="alex"
    2 v=test.swapcase()
    3 print(v)
    1 >>ALEX

    6.center:设置宽度,并将内容居中

    1 # *空白未知填充
    2 test="asd"
    3 v=test.center(20)   # 20指代长度
    4 v1=test.center(20,"9")
    5 print(v,v1)
    1 >>        asd          99999999asd999999999

    7.(ljust(左),rjust(右)):随意指定宽度,进行字符填充

    1 test="asd"
    2 v=test.ljust(20,"%")
    3 v1=test.rjust(20,"%")
    4 print(v)
    5 print(v1)
    6 
    7 >>asd%%%%%%%%%%%%%%%%%
    8 >>%%%%%%%%%%%%%%%%%asd

    8.zfill:字符串填充,不能指定字符填充

    1 test="123"
    2 v=test.zfill(20)
    3 print(v)
    1 >>00000000000000000123

    9.counter:去字符串中寻找子序列出现的次数

    1 test="alasddslaw"
    2 v=test.count("a")
    3 v1=test.count('sd')
    4 v2=test.count('a',5)       #表示从第五个开始找
    5 v3=test.count('a',5,6)      #表示从第五个开始找,到第六个结束
    6 print(v,v1,v2,v3)
    7 
    8 >>3 1 1 0

    10.(endswith,startswith):判断定结尾开始

    1 test="abc"
    2 v=test.endswith('c')
    3 v=test.startswith('a')
    4 print(v)
    5 
    6 >>True

    11.expendtabs:断句

    1 test='username	email	password
    xiaozhou	www@q.com	123
    xiaoli	qqq@.com	345
    xiaowang	sdfjd@.com	789
    '
    2 v=test.expandtabs(20)
    3 print(v)
    4 
    5 >>
    6 username            email               password
    7 xiaozhou            www@q.com           123
    8 xiaoli              qqq@.com            345
    9 xiaowang            sdfjd@.com          789

    12.分割,只根据:true,false来确认是否保留换行

    1 test="assdsfdg
    sjfdhsdef
    ksf"
    2 v1=test.splitlines(False)
    3 v2=test.splitlines(True)
    4 print(v1)
    5 print(v2)
    6 
    7 >>['assdsfdg', 'sjfdhsdef', 'ksf']
    8 >>['assdsfdg
    ', 'sjfdhsdef
    ', 'ksf']

    13.find:寻找字符串,找不到输出-1

    1 test="alexalex"
    2 # 如果未找到,输出结果:-1
    3 v=test.find('x')
    4 print(v)
    5 
    6 >>3

    14.index:索引,找不到就报错

    1 test="qwert"
    2 v=test.index('q')
    3 print(v)
    4 
    5 >>0

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

    1 test='i am {name},age{a}'
    2 v=test.format(name='xiaowang' ,a='18')
    3 print(v)
    4 
    5 >>i am xiaowang,age18

    16.isalnum:字符串中是否只包含字母和数字

    1 test="123"
    2 v=test.isalnum()
    3 print(v)
    4 
    5 >>True

    17.isidentifier:判断字母,下划线,标识符

    1 a="def"
    2 v=a.isidentifier()
    3 print(v)
    4 
    5 >>True

    18.(isdecimal,isdigit,isnumeric):判断是否为数字

    1 test=input('please enter  int or str:') #例如:1,二
    2 v1=test.isdecimal()
    3 v2=test.isdigit()
    4 v3=test.isnumeric() #支持中文数字
    5 print(v1,v2,v3)

    19.keyword模块:显示当前版本所有关键字

    1 import keyword
    2 keyword.kwlist   #在命令窗口中输出

    20.isprintable:判断是否显示不可显示的字符,否则无法输出, 制表符, 换行

    1 test="asdfghj"
    2 v=test.isprintable()
    3 print(v)
    4 
    5 >>True

    如有不足,欢迎指正!

     

  • 相关阅读:
    237. Delete Node in a Linked List
    430. Flatten a Multilevel Doubly Linked List
    707. Design Linked List
    83. Remove Duplicates from Sorted List
    160. Intersection of Two Linked Lists
    426. Convert Binary Search Tree to Sorted Doubly Linked List
    142. Linked List Cycle II
    类之间的关系
    初始化块
    明确类和对象
  • 原文地址:https://www.cnblogs.com/wangwenchao/p/11330705.html
Copyright © 2011-2022 走看看