zoukankan      html  css  js  c++  java
  • Python全栈Day 10部分知识点

    in/not in:结果为布尔值。

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 #'胡彦斌' 字符串
     4 #'胡'字符
     5 #'胡彦斌' 彦斌:子字符串,也叫子序列
     6 #ctrl+? Pycharm中的整体注释
     7 name='胡彦斌'
     8 if '胡斌' in name:
     9     print('ok')
    10 else:
    11     print('Error')

    结果为Error(必须是连续的字符,才能输出ok)

    运算符!=和<>都是不等于,但前者更常用 

    基本数据类型:(数字、字符串、布尔值、列表、元祖、字典)

      1.数字 

        - int

        将字符串转换为数字   

    1 #!/usr/bin/env python
    2 # -*- coding:utf-8 -*-
    3 a='123'
    4 print(type(a),a)
    5 b=int(a)
    6 print(type(b),b)

        输出    

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

        把num以16进制的形式转化为10进制

    1 #!/usr/bin/env python
    2 # -*- coding:utf-8 -*-
    3 num='a' 
    4 v=int(num,base=16)
    5 print(v)

       

        输出

          10

        - bit_length

    1 #!/usr/bin/env python
    2 # -*- coding:utf-8 -*-
    3 age=1
    4 n=age.bit_length()  #当前数字的二进制,至少用n位表示
    5 print(n)

        输出

          1

      2.字符串

        - capitalize

    1 #!/usr/bin/env python
    2 # -*- coding:utf-8 -*-
    3 test='chenYUAN'
    4 v=test.capitalize()
    5 print(v)

        输出

          Chenyuan

        - casefold ; - lower

    1 ...
    2 #所有的变小写,casefold更好,很对未知对应关系变小写
    3 v1=test.casefold()
    4 print(v1)
    5 v2=test.lower()
    6 print(v2)

        输出

          chenyuan

          chenyuan

         - center

    1 ...
    2 #设置宽度将内容居中
    3 #参数20代指总长度
    4 # *代表空白位置填充,一个字符,可有可无
    5 v3=test.center(20,'*')
    6 print(v3)

        输出

          ******chenYUAN******

        - count

    1 ...
    2 #去字符串中,寻找子序列出现次数
    3 test1='alexalex'
    4 v4=test1.count('ex')
    5 v5=test1.count('ex',5)  #字符串第一位序号0,从序号5开始找
    6 v6=test1.count('ex',5,6)    #找至序号6停止
    7 print(v4)
    8 print(v5)
    9 print(v6)

        输出

          2

          1

          0

        - endswith ; - startswith

     1 ...
     2 #'chenYUAN'中某段以……结尾/开始
     3 v8=test.endswith('en',0,3) #序号4不算在内
     4 v9=test.endswith('en',0,4)
     5 v10=test.startswith('he',1,2) #序号2不算在内
     6 v11=test.startswith('he',1,3)
     7 print(v8)
     8 print(v9)
     9 print(v10)
    10 print(v11)

        输出     

          False
          True
          False
          True

        - find ; - index

    1 ...
    2 #从开始往后找,找到第一个之后,获取其位置
    3 test2='alexalex'
    4 v12=test2.find('ex',5,7)
    5 v13=test2.find('ex',5,8)
    6 print(v12)
    7 print(v13)

    '''#index找不到会报错,忽略
      test='alexalex'
      v=test.index('8')
      print(v)'''

        输出

          -1  (没找到)

          6  (在序号6找到了)

          '''

          Traceback (most recent call last):
          File "<input>", line 2, in <module>
          ValueError: substring not found

          '''

        - format

    1 ...
    2 test3='I am {name},age {a}'
    3 print(test3)
    4 v14=test3.format(name='alex',a=19)
    5 print(v14)
    '''test3='I am {1},age {0}'
    print(test3)
    v14=test3.format(19,'alex')
    print(v14)'''

    '''
    test3='I am {name},age {a}'
    print(test3)
    v14=test3.format_map({"name":'alex',"a":19})  #注意里面是dict
    print(v14)'''

        输出

          I am {name},age {a}
          I am alex,age 19

          '''

          I am {1},age {0}
          I am alex,age 19

          '''

          '''    

          I am {name},age {a}
          I am alex,age 19

          '''

        - isalnum

    1 ...
    2 #字符串中是否只包含字母和数字
    3 test4='ab123'
    4 test5='a+b=..1?'
    5 v15=test4.isalnum()  #isalnum is alpha/number?
    6 v16=test5.isalnum()
    7 print(v15)
    8 print(v16)

        输出

          True

          False

        

  • 相关阅读:
    在Vue-cli3.x中引入element-ui的新方式
    通过JS屏蔽鼠标右键
    java异常有效实践
    设计之禅——迭代器模式
    设计之禅——状态模式
    设计之禅——外观模式
    设计之禅——适配器模式
    设计之禅——我只要结果(命令模式)
    设计之禅——装饰者模式详解(与代理模式的区别以及与其他模式的组合)
    设计之禅——生成器模式
  • 原文地址:https://www.cnblogs.com/chenyuan-1995/p/9638578.html
Copyright © 2011-2022 走看看