zoukankan      html  css  js  c++  java
  • 【python学习笔记01】python的数据类型

    python的基本数据类型


     整型 int 

    浮点型 float 

    真值 bool

    字符串 str

    列表 list       #[1,2,3]

    元组 tuple    #(1,2,3)

    字典 dict      #{1:2}

    集合 set       #set{['a','c','b']} 集合对象是一组无序排列的可哈希的值:集合成员可以做字典的键

    字符串操作方法


    1、大小写变换

    >>>a = "HEllo"
    >>>a.lower() #小写
    'hello'
    >>>a.upper() #大写
    'HELLO'
    >>>a.swapcase() #交换大小写
    'heLLO'
    >>>a.title() #标题化字符串,所有单词以大写字母开始,其他小写
    'Hello'
    >>>a.capitalize() #首字母大写,其余小写
    'Hello'

    2、字符串运算

    + 字符串连接

    >>> a = 'hello'
    >>> b = 'world'
    >>> print(a+b)
    'helloworld'

    * 重复输出

    >>> a = 'abc'
    >>> print(a*3)
    'abcabcabc'

    [] 索引获取字符

    >>> a = 'hello'
    >>> print(a[2])
    l
    >>> print(a[0:2])
    he

    in 成员运算符,如果字符串中包含给定字符返回True

    not in 成员运算符,如果字符串中不包含给定字符返回True

     r/R 原始字符串,没有转义特殊或不能打印

    >>> print(r'
    abc
    ')
    
    abc
    
    >>> print(R'
    abc
    ')
    
    abc
    

    % 格式字符串

    3、格式字符串

    %c 格式化字符及其ASCII码

    %s 格式化字符串

    %d 格式化整数

    %u 格式化无符号整型

    %o 格式化无符号八进制数

    %x 格式化无符号十六进制数

    %X 格式化无符号十六进制数(大写)

    %f 格式化浮点数字,可指定小数点后的精度

    %e 用科学计数法格式化浮点数

    %E 作用同%e,用科学计数法格式化浮点

    %g 根据值的大小决定使用%f或%e

    %G 作用同%g,根据值的大小决定使用%f或%e

    %p 用十六进制数格式化变量的地址

    格式化操作符辅助:

    * 定义宽度或小数精度

    - 用作左对齐

    + 正数前面显示+号

    <sp>正数前面显示空格

    #八进制前显示(‘0’),十六进制前显示‘0x'或者'0X',取决于用x或X

    0 显示数字前面填充'0'而不是默认空格

    % '%%'输出一个单一的’%‘

    (var) 映射变量(字典参数)

    m.n m显示最小宽度,n小数点后的位数

    4、三引号

    ''' '''三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符

    5、Unicode字符串

    >>> u'Hellou0020World !'
    u'Hello World !'

    插入编码值为 0x0020 的 Unicode 字符(空格符)

    6、字符串的内建函数

    >>> s = 'hello world'
    >>> s.find('el')
    1
    >>> s.find('el',0,3)
    1

    返回出现子字符串的第一个字母标号,如果没有返回-1;

    s.find(substr,beg,end) 返回包含在beg end指定范围内的索引值

    >>> 'This is a test'.replace('is', 'eez')  
    'Theez eez a test'  

    返回字符串所有匹配项被替换后得到的字符串

    >>> '1+2+3+4+5'.split('+')  
    ['1', '2', '3', '4', '5']  
    >>> '/usr/bin/env'.split('/')  
    ['', 'usr', 'bin', 'env']  
    >>> 'Using the default'.split()  
    ['Using', 'the', 'default'] 

    将字符串分割成序列

    >>> seq = ['1', '2', '3', '4', '5']  
    >>> sep = '+'  
    >>> sep.join(seq)  
    '1+2+3+4+5'  
    >>> dirs = '', 'usr', 'bin', 'env'  
    >>> '/'.join(dirs)  
    '/usr/bin/env'  

    join在列表中添加元素

    >>> '         internal whitespace is kept        '.strip()  
    'internal whitespace is kept'  

    strip去除两侧(不含内部)空格的字符串

    s.join

    s.center(width)

    s.count(str,beg,end)

    s.index(str,beg,end) #和find方法一样,str不在报异常

    s.isalnum() #至少一个字符且所有字符为字母或数字返回True

    s.isdecimal()

    s.islower()

    s.isnumeric()

    s.isspace()

    s.istitle()

    s.isupper()

    s.digits:包换数字 0 - 9 的字符串

    s.letters:包含所有字母(大写和小写)的字符串

    s.lowercase:包含所有小写字母的字符串

    s.printable:包含所有可打印字符的字符串

    s.punctuation:包含所有标点的字符串

    s.uppercase:包含所有大写字母的字符串


    ps:字符串的方法非常多,需要在实例中熟练掌握

  • 相关阅读:
    Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor
    Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
    IdentityServer4 And AspNetCore.Identity Get AccessToken 问题
    Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
    Java Spring Boot VS .NetCore (七) 配置文件
    Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
    Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
    Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
    Java Spring Boot VS .NetCore (三)Ioc容器处理
    Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
  • 原文地址:https://www.cnblogs.com/etduino/p/4576070.html
Copyright © 2011-2022 走看看