zoukankan      html  css  js  c++  java
  • day07--基本数据类型的常见使用方法

    一、int类型与float类型


    1、将纯数字类型转换成int

    >>> res = int('1024')
    >>> print(res,type(res))
    1024 <class 'int'>
    

     2、进制之间的转换

    十进制转换成二进制

    14 -->  1 1 1 0
    >>> print(bin(14))
    0b1110
    >>> 
    >>> print(int('0b1110',2))
    14
    >>> 
    

     十进制转八进制

    14 --> 16
    >>> print(oct(14))
    0o16
    >>> 
    >>> print(int('0o16',8))
    14
    >>> 
    

     十进制转十六进制

    14 --> e
    >>> print(hex(14))
    0xe
    >>> 
    
    >>> print(int('0xe',16))
    14
    >>> 

    进制运算主要用于数学运算和比较运算

    二、字符串的内置方法


     1、字符串可以把其他任意类型的数据都转换成字符串

    >>> msg = 'hello'
    >>> print(msg,type(msg))
    hello <class 'str'>
    >>> 
    
    >>> obj = str({'egon':19})
    >>> print(obj,type(obj))
    {'egon': 19} <class 'str'>
    >>> 
    
    >>> age = 19
    >>> res = str(age)
    >>> print(res,type(res))
    19 <class 'str'>
    >>> 

     2、字符串的内置方法

    • 取值(正向取,反向取,对于字符串只能进行读操作):
      正向取值
      >>> msg = 'hello world'
      >>> print(msg[0],msg[8])
      h r
      
      反向取值
      >>> print(msg[-1],msg[-3])
      d r
      >>> 
    • 切片(从一个大字符串中拷贝出一个小字符串)
      正向取
      msg = 'hello world'
      >>> res = msg[0:5:1]
      >>> print(res,'
      ',msg)
      hello 
       hello world
      >>> 
      
      反向取
      >>> res = msg[5:0:-1]
      >>> print(res)
       olle
      >>> 
      
      >>> res = msg[-1::-1]
      >>> print(res)
      dlrow olleh
      >>> 
    • 长度len
      >>> print(len(msg))
      11
      >>> 
    • 成员运算in 和 not in
      >>> print('egon' in 'egon is very nb')
      True
      >>> 
      
      >>> print('tank' not in 'egon is very nb')
      True
      >>> 
    • 移除字符串左右两侧的符号(默认为空格)
      >>> name = '  egon  '
      >>> print(name.strip(),name)
      egon   egon  
      >>> 
      
      >>> age_str = '****18***'
      >>> print(age_str.strip('*'),age_str)
      18 ****18***
      >>> 
    • 切分split(默认分割符为空格)
      >>> str_obj = 'hellow egon, you are very nb'
      >>> print(str_obj.split(),'
      ',str_obj)
      ['hellow', 'egon,', 'you', 'are', 'very', 'nb'] 
       hellow egon, you are very nb
      >>> 
      
      指定字符分割
      >>> userfile_path = '/opt/nubosh/vmsec/upload'
      >>> print(userfile_path.split('/'),'
      ',userfile_path)
      ['', 'opt', 'nubosh', 'vmsec', 'upload'] 
       /opt/nubosh/vmsec/upload
      >>> 
    • 需要掌握
      1、strip、lstrip、rstrip
      >>> name = ' egon '
      >>> print(name.strip())
      egon
      >>> print(name.lstrip())
      egon 
      >>> print(name.rstrip())
       egon
      
      2、lower、upper
      >>> obj = 'egon'
      >>> print(obj.upper())
      EGON
      >>> print(obj.lower())
      egon
      >>> 
      
      3、startswith、endswith
      >>> file_path = '/opt/nubosh/vmsec/vmsec.db'
      >>> print(file_path.endswith('db'))
      True
      >>> print(file_path.startswith('/'))
      True
      >>> 
      
      4、split、rsplit(指定分割次数)
      ['/opt/nubosh/vmsec', 'vmsec.db']
      >>> print(file_path.split('/',1))
      ['', 'opt/nubosh/vmsec/vmsec.db']
      >>> 
      
      5、format
      
      6、join字符串拼接
      file_path = '/opt/nubosh/vmsec/vmsec.db'
      >>> path_list = file_path.split('/')[0:-1:1]
      >>> print('/'.join(path_list+['log.txt']))
      /opt/nubosh/vmsec/log.txt
      >>> 
      
      7、replace
      >>> import time
      >>> current_time = time.ctime(time.time())
      >>> print(current_time)
      Tue Mar 10 16:57:26 2020
      >>> res = current_time.replace(':','/')
      >>> print(res)
      Tue Mar 10 16/57/26 2020
      >>> 
      
      8、判断字符串是否由纯数字组成
      >>> print('123'.isdigit())
      True
      >>> print('12.3'.isdigit())
      False
      >>> 
    • 其他了解类型
      # 4.3了解
      
      #4.3.1、find,rfind,index,rindex,count
      
      msg='hello egon hahaha'
      
      # 找到返回起始索引
      
      # print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
      
      # print(msg.find('egon'))
      
      # print(msg.index('e'))
      
      # print(msg.index('egon'))
      
      # 找不到
      
      # print(msg.find('xxx')) # 返回-1,代表找不到
      
      # print(msg.index('xxx')) # 抛出异常
      
       
      
      # msg='hello egon hahaha egon、 egon'
      
      # print(msg.count('egon'))
      
       
      
      #4.3.2、center,ljust,rjust,zfill
      
      # print('egon'.center(50,'*'))
      
      # print('egon'.ljust(50,'*'))
      
      # print('egon'.rjust(50,'*'))
      
      # print('egon'.zfill(10))
      
       
      
      #4.3.3、expandtabs
      
      # msg='hello	world'
      
      # print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
      
       
      
      #4.3.4、captalize,swapcase,title
      
      # print("hello world egon".capitalize())
      
      # print("Hello WorLd EGon".swapcase())
      
      # print("hello world egon".title())
      
       
      
      #4.3.5、is数字系列
      
      #4.3.6、is其他
      
      # print('abc'.islower())
      
      # print('ABC'.isupper())
      
      # print('Hello World'.istitle())
      
      # print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
      
      # print('ad'.isalpha()) # 字符串由由字母组成结果为True
      
      # print('     '.isspace()) # 字符串由空格组成结果为True
      
      # print('print'.isidentifier())
      
      # print('age_of_egon'.isidentifier())
      
      # print('1age_of_egon'.isidentifier())
      
       
      
       
      
      num1=b'4' #bytes
      
      num2=u'4' #unicode,python3中无需加u就是unicode
      
      num3='' #中文数字
      
      num4='' #罗马数字
      
       
      
      # isdigit只能识别:num1、num2
      
      # print(num1.isdigit()) # True
      
      # print(num2.isdigit()) # True
      
      # print(num3.isdigit()) # False
      
      # print(num4.isdigit()) # False
      
       
      
       
      
       
      
      # isnumberic可以识别:num2、num3、num4
      
      # print(num2.isnumeric()) # True
      
      # print(num3.isnumeric()) # True
      
      # print(num4.isnumeric()) # True
      
       
      
      # isdecimal只能识别:num2
      
      print(num2.isdecimal()) # True
      
      print(num3.isdecimal()) # False
      
      print(num4.isdecimal()) # False
  • 相关阅读:
    spring
    google-c-style
    process想停就停,真爽
    mytop
    Java 图片设置圆角(设置边框,旁白)
    当setTimeout遇到闭包
    FreeMarker辅助
    ImageIO.wtrie生成jpg图片质量损失方案:BufferedImage生成jpg图片文件流
    从BufferedImage到InputStream,实现绘图后进行下载(生成二维码图片并下载)
    使用Javascript 实现类
  • 原文地址:https://www.cnblogs.com/surpass123/p/12456826.html
Copyright © 2011-2022 走看看