• Python_基础(数据类型)


    一,字符串 str

      字符串中常见的方法

      1.test.capitalize()  # 将字符串的首字符大写

      2.test.lower()        # 将字符串的全部元素转成i小写形式

      3.test.casefole()   # 将字符串的全部元素转成i小写形式(可将多种形式的字符转成小写,功能强于lower() )

      4.test.center(20,“*”)    # 将字符串的长度转为20,并将其居中,并利用 * 进行填充(默认用空格进行填充)

    >>> test = "hello"
    >>> print(test.center(20,"*"))
    
    # 输出结果
    *******hello********

      5.test.count("a",start=None,end=None)  # 输出字符串test中元素 a 的个数(开始位置start,结束位置end)

      6.test.endswith("a")    # 检查字符串是否以 a 为结尾,返回布尔类型(是则返回True)

      7.test.startswith("a")   #检查字符串是否以 a 为开始,返回布尔类型(是则返回True)

      8.test.find("a")       # 从开始查找子字符串,找到第一个后返回其位置,未找到时程序返回 -1

      9.test.index("a")    # 从开始查找子字符串,找到第一个后返回其位置,未找到时程序报错

      10.format简答用法

    # 替换中括号中的内容
    >>> test = "I am {},age {}"
    >>> print(test)
    I am {},age {}
    >>> test_a = test.format("henry",18)
    >>> print(test_a)
    I am henry,age 18
    >>> 
    
    # 替换指定的元素 >>> test = "I am {name},age {age}" >>> print(test) I am {name},age {age} >>> test_a = test.format(name="henry",age=18) >>> print(test_a) I am henry,age 18 >>>

      11.test.format_map({"name":"henry","age":18})

    test = "I am {name},age {age}"
    
    # 以字典的形式进行格式化
    test_a = test.format_map({"name":"henry","age":18})
    print(test_a)
    
    # 输出为
    I am henry,age 18

     

      12.test.islnum()  # 判断一个字符串是否为字母数字字符串(纯字母,纯数字,字母数字组合),是则返回True

      13.test.expandtabs()  # 将字符串以指定的间隔进行分割,当遇到字符串中的 时,则利用剩下的空格来进行填充

      

    test = "123	456	712	34561	2345"
    print(test.expandtabs(6))
    
    # 输出为:
    123   456   712   34561 2345
    
    
    # 作用 进行制表
    test = "hadh	adfa	dfa	dfdf	jhjhj	fsd
    hadh	adfa	dfa	dfdf	jhjhj	fsd
    hadh	adfa	dfa	dfdf	jhjhj	fsd
    "
    
    print(test.expandtabs(6))
    
    # 输出为
    hadh  adfa  dfa   dfdf  jhjhj fsd
    hadh  adfa  dfa   dfdf  jhjhj fsd
    hadh  adfa  dfa   dfdf  jhjhj fsd

     

     

      14.test.isalpha()  # 判断字符串中是否只含有字母(不含其它字符),是则返回True

      15.test.isdecimal()  # 判断字符串中是否只含有数字(不含有其它元素),是则返回True

      16.test.isdigit()  # 判断字符串中是否只含有数字(不含有其它元素)(可判断②),是则返回True

      17.test.isnumeric()  # 判断字符串中是否只含有数字(不含有其它元素)(可判断中文 二),是则返回True

      18.test.isidentfire()  # 判断字符串中是否自含有字母,数字,下划线,(是则返回True)

      19.test.islower()  # 判断字符串是否全为小写

      20.test.isupper()  # 判断字符串是否全为大写

      21.test.upper()  # 将字符串只中的元素全部变为大写

      22.test.isprintable()  # 判断字符串中是否有不可显示字符(        等)

      23.test.isspace()  # 判断字符串中是否只包含空格,一个或则多个

      24.test.title()  # 将一个字符串中所有单词的首字母全部大写

      25.test.istitle()  # 判断一个字符串是否为标题,即字符串中的元素全为大写

      26.join  # 将字符串中的每个字符按照指定过分隔符进行分割

    >>> test = "helloworld"
    >>> test_new = "_".join(test)
    >>> print(test_new)
    h_e_l_l_o_w_o_r_l_d
    >>> 

       27.test.ljust(n,"_")  # 将字符串test放于左边,用指定的字符在右边填充剩下的内容

      28.test.rjust(n,"_")  # 将字符串test放于右边,用指定的字符在左边填充剩下的内容

    >>> test = "hello"
    >>> print(test.ljust(20,"*"))
    hello***************
    >>> print(test.rjust(20,"*"))
    ***************hello
    >>> 

       29.test.strip()  # 去除两边的空白(默认去除空白),括号中可指定的子序列,用与去除的指定内容

      30.test.lstrip()  # 只去除左边指定字符

      31.test.rstrip()  # 只去除右边指定字符

    >>> test = "hello"
    >>> print(test.strip("h"))
    ello
    >>> print(test)
    hello
    >>> print(test.rstrip("o"))
    hell
    >>> print(test.rstrip("ol"))
    he
    >>> print(test.rstrip("lo"))
    he
    >>> print(test.lstrip("he"))
    llo
    >>> 

       32.test.partition("x")  # 将字符串test以指定子字符串x(左边第一个),进行分割,分成三份{“aaa”,“x”,“bbb”}

      33.test.rpartition("x")  # 从右开始找,第一个

    >>> test = "dfawejjijin"
    >>> print(test.partition("w"))
    ('dfa', 'w', 'ejjijin')
    >>> 
    >>> print(test.rpartition("j"))
    ('dfawejji', 'j', 'in')

       34.maketrans与translate配合使用

    >>> test_str = "helloworld"
    >>> test_m = str.maketrans("hwo","123")  # 将字符串中的 "h"替换成"1"...
    >>> new_test_str = test_str.translate(test_m)
    >>> print(new_test_str)
    1ell323rld
    >>> 

       35.test.split("s",2)  # 根据字符s对字符串test进行分割,当后方填入2时,表示只利用前两个 s 进行分割

    >>> test = "aaahbbbhccch"
    >>> print(test.split("h"))
    ['aaa', 'bbb', 'ccc', '']
    >>> print(test.split("h",1))
    ['aaa', 'bbbhccch']
    >>> print(test.split("h",2))
    ['aaa', 'bbb', 'ccch']
    >>> 

       36.test.splitlines()  # 根据字符串中的换行符进行分割,括号中可加入True或False(为True时可保留换行符 )

    >>> test = "dsfaf
    dfasdf
    erwerw
    "
    >>> print(test.splitlines())
    ['dsfaf', 'dfasdf', 'erwerw']
    >>> print(test.splitlines(True))
    ['dsfaf
    ', 'dfasdf
    ', 'erwerw
    ']
    >>> print(test.splitlines(False))
    ['dsfaf', 'dfasdf', 'erwerw']
    >>> 

       37.test.startswith("xx")  # 字符串test是否以字符串"xx"为开头,是则返回True

      38.test.endswith("xx")  # 字符串test是否以字符串"xx"为结尾,是则返回True

    >>> test = "helloworld"
    >>> print(test.startswith("h"))
    True
    >>> print(test.startswith("he"))
    True
    >>> print(test.endswith("d"))
    True
    >>> print(test.endswith("ld"))
    True

       39.test.replace("he","bbb")  # 将字符串中的所有"he"全部替换成"bbb"

      40.索引

    >>> test = "hello"
    >>> a = test[1]
    >>> a
    'e'
    >>> b = test[0:2]    # 取头不取物尾
    >>> b
    'he'
    >>> c = test[0:-1]       # 从第一个开始,到倒数第一个结束 
    >>> c
    'hell'
    >>>

       41.注:

    字符串一旦创建就会在内存中创建该字符串(在内存中创建一个连续的空间),

    一旦创建就无法修改,一旦修改了,就会在另外的内存区域中创建该字符串;

    如果对原来的字符串进行修改 代价太大(要将所有的字符串进行移动),一旦修改或拼接 会产生新串;

    Name = hello     #在内存中创建该字符串

    Age = 18            #在内存中创建该字符串

    Info = name + age             # 两个相加 会在内储存中创建一个新的字符串空间

       42.range()  # 创建一个连续的数字

    >>> test = range(10)
    >>> for item in test:
    ...     print(item)
    ... 
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> 
    # 注:在python2.7中,当test = range(100)时,会立刻在内存中创建0~99的数并输出

        >>> range(10)
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    # 在python3.5中,只有进行循环时,才进行创建

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

      

     

  • 相关阅读:
    java ElasticSearch并发操作之乐观锁的使用
    ES并发更新导致冲突的问题
    012 found ' ' @ line 30, column 50. 'com.google.code.gson:gson:2.7
    011 Error:Failed to resolve: com.android.support:recyclerview-v7:28.0.0解决方法
    010 Failed to resolve: com.squareup.okhttp3:okhttp:3.4.1
    009 A problem occurred configuring project ':app'. > Failed to notify project evaluation listener. >
    008 导入项目每次都要Gradle:Download https://services.gradle.org/distributions/gradle
    007 Android Studio错误Error:(23, 17) 解决方案
    006 Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"解决办法
    005 Android Studio 快捷键大全
  • 原文地址:https://www.cnblogs.com/Doaoao/p/9980199.html
Copyright © 2020-2023  润新知