zoukankan      html  css  js  c++  java
  • strin 字符串的内置函数

    count("x")统计字符串的元素的个数
    a = "hello kitty"
    print (a.count("t"))#统计t出现的个数
    输出结果:
    2
    endswith("x")判断以某个字符结尾
    a = "python"
    print (a.endswith("n"))
    print (a.endswith("on"))
    print (a.endswith("hon"))
    print (a.endswith("nn"))
    输出结果:
    True
    True
    True
    False
    startswith("x")判断以某个字符开头
    a = "python"
    print (a.startswith("p"))
    print (a.startswith("py"))
    print (a.startswith("pyt"))
    print (a.startswith("pp"))
    输出结果:
    True
    True
    True
    False
    capitalize()把字符串首写字母大写
    a = "python"
    print (a.capitalize())
    输出结果:
    Python
    upper()将字符串里的小写变成大写
    a = "python"
    print (a.upper())
    输出结果:
    PYTHON
    lower()把字符串大写变成小写
    a = "PYTHON"
    print (a.lower())
    输出结果:
    python
    swapcase()把字符串里的大小写反转
    a = "PyThoN"
    print (a.swapcase())
    输出结果:
    pYtHOn
    isupper()判断字符串是否大写
    a = "python"
    a1 = "PYTHON"
    
    print (a.isupper())
    print (a1.isupper())
    输出结果:
    False
    True
    islower()判断字符串是否小写
    a = "python"
    a1 = "PYTHON"
    print (a.islower())
    print (a1.islower())
    输出结果:
    True
    False
    isalnum()-判断字符串是否是字母或者数字
    a = "python"
    a1 = "123"
    a2 = "python123"
    a3 = "中国"
    a4 = "#$###"
    print (a.isalnum())
    print (a1.isalnum())
    print (a2.isalnum())
    print (a3.isalnum())
    print (a4.isalnum())
    输出结果:
    True
    True
    True
    True
    False
    istitle()判断首写字母是否为大写
    a = "python"
    a1 = "Python"
    a2 = "PYTHON"
    
    print (a.istitle())
    print (a1.istitle())
    print (a2.istitle())
    输出结果:
    False
    True
    Fals
    strip()把字符串左右空格去掉
    a = " python "
    print (a.strip())
    输出结果:
    python #已经将左右空格去掉了,这样看不出来
    ----------------------
    strip()也会把
    换行去掉
    a = "python
    "
    a1 = "hello"
    print (a.strip()) #strip()把换行去掉了
    print (a1)
    输出结果:
    python
    hello
    ----------------------
    a = "python
    "
    a1 = "hello"
    print (a)#不使用strip(),将会换行
    print (a1)
    输出结果:
    python
    
    hello
    lstrip()去掉字符串左空格
    a = " python "
    print (a.strip())
    输出结果:
    python  #最后面这有空格,这样看不出来
    rstrip()去掉字符串右空格
    a = "  python   "
    print (a.rstrip())
    输出结果:
      python  #已经将右边空格去掉了,这看不出来
    replace(old,new) 把字符串的内容替换掉
    a = "my tang"
    print (a.replace("tang", "guo"))
    输出结果:
    my guo
    split(分割符,分割次数)对字符串进行各种分割
    a = "my 
    tang         guo
    li"
    print (a.split())#不指定以什么分割的情况下,会默认所有的空格,换行,制表符分割
    输出结果:
    ['my', 'tang', 'guo', 'li']
    -------------------------------------
    a = "my tang guo"
    print (a.split(" ")) #以空格进行分割
    输出结果:
    ['my', 'tang', 'guo']
    --------------------------------------
    a = "my tang guo li"
    print (a.split(" ",2))#以空格进行分割2次
    输出结果:
    ['my', 'tang', 'guo li']
    ---------------------------------------
    a = "my tang guo li"
    print (a.split("a"))#以 a进行分割,a会被切掉
    输出结果:
    ['my t', 'ng guo li']
    --------------------------------------
    可以指定多个字符进行分割
    a = "my tang guo li"
    print ((a.split("an"))#以 an进行分割,an会被切掉
    ["my t" ,"g guo li"]
    ----------------------------------------
    isdigit()判断字符串是否为整型
    a = "python"
    a1 = "123"
    print (a.isdigit())
    print (a1.isdigit())
    输出结果:
    True
    False
    rfind("x")查找元素所在的索引位置,从右向左开始找,找到的第一个元素位置
    a = "htp"
    a1 = "http"
    print (a.rfind("t"))
    print (a1.rfind("t"))
    输出结果:
    1
    2
    -----------------------------------------------------------------
    find("x")从左向右开始找元素,找到的第一个位置,并返回索引位置
    a = "htp"
    a1 = "http"
    print (a.find("t"))
    print (a1.find("t"))
    输出结果:
    1
    1
    index("x")从左向右开始找元素的位置,
    a = "ppython"
    print (a.index("p")) #有多个相同元素时,返回的是第一个找到的索素值
    输出结果:
    0
    find 与 index看上去功能一样,其实是有区别的
    a = "python"
    print (a.index(x))
    index 在没有找到子串的时候会报错(
    ValueError: substring not found),影响程序执行。
    print (a.find("x"))
    find 在没有找到子串时,不会报错,而是会返回-1所以不会影响执行。
  • 相关阅读:
    HDU 5640 King's Cake
    HDU 5615 Jam's math problem
    HDU 5610 Baby Ming and Weight lifting
    WHU1604 Play Apple 简单博弈
    HDU 1551 Cable master 二分
    CodeForces659C Tanya and Toys map
    Codeforces 960E 树dp
    gym 101485E 二分匹配
    Codeforces 961E 树状数组,思维
    Codeforces Round #473 (Div. 2) D 数学,贪心 F 线性基,模板
  • 原文地址:https://www.cnblogs.com/guog1/p/8367274.html
Copyright © 2011-2022 走看看