zoukankan      html  css  js  c++  java
  • python笔记之str常用方法

    #Auther Bob
    #--*--conding:utf-8 --*--
    # s1 = 'aBcdE1d'
    # =========================================================================================
    # str.capitalize() 把s1这个字符才的大写字母变为小写字母,小写字母变为大写字母
    # print(s1.capitalize())
    # Abcded


    # =========================================================================================
    # str.casefold() #把字符串s1中的所有字母变为小写字母
    # print(s1.casefold())
    # abcde1d

    #=========================================================================================
    # str.center() 让字符串的占的长度为20,且把字符串s1放在中间,如果字符串s1不够20个字符,则用'-'号来代替
    # print(s1.center(20,'-'))
    # ------aBcdE1d-------

    # ===========================================================================================
    # str.count(), 统计一个字符串中某个字符出现的次数
    # s2 = "12AAbbCCCCC"
    # print(s2.count('1'))
    # print(s2.count('C'))
    # print(s2.count('c'))
    # 1
    # 5
    # 0
    # ===========================================================================================
    # str.endswith(), 检查某个字符串是否以某个或者某几个字符串结尾
    # s2 = 'aAAABbcde'
    # print(s2.endswith('e'))
    # print(s2.endswith('cde'))
    # print(s2.endswith('E'))

    # True
    # True
    # False
    # =============================================================================================
    # str.find() 判断字符串中是否有某个或者某几个字符,如果有则返回该字符的索引,如果没有则返回-1
    # print(s2.find('A'))
    # print(s2.find('F'))

    # 1
    # -1
    #==============================================================================================
    # str.index() 查看字符串中某个字符或者某几个字符的的索引,且只会显示第一个字符的索引
    # s2 = 'aAAABbcde'
    # print(s2.index('a'))
    # print(s2.index('A'))
    # 0
    # 1

    #==============================================================================================
    # str.isalnum() 判断字符串中是否均为字母和数字,如果是则返回为True,如果不是则返回False
    # s1 = 'aBcdE1d'
    # s2 = 'ad_+1'
    # print(s1.isalnum())
    # print(s2.isalnum())

    # True
    # False

    #==============================================================================================
    # str.isalpha() 判断字符串是否全部为字母,如果是则为True,如果不是,则为False
    # s1 = 'aBcdEd'
    # s2 = '123'
    # print(s1.isalpha())
    # print(s2.isalpha())

    # True
    # False

    #==============================================================================================
    # str.isdecimal()

    #==============================================================================================
    # str.isdigit() 判断字符串是否全部为数字,如果是则返回True,如果不是,则返回False
    # s1 = '1aBcdEd'
    # s2 = '123'
    # print(s1.isdigit())
    # print(s2.isdigit())

    # False
    # True


    #==============================================================================================
    # str.istitle() 判断字符串中的每个单词的首字母是否大写,如果是则返回True,如果不是,则返回False
    # s1 = 'A Bod Stuedent Is'
    # s2 = 'A bdd sde Is'
    # print(s1.istitle())
    # print(s2.istitle())

    # True
    # False

    #==============================================================================================
    # str.join() 用来连接序列中的字符串
    # l1 = ['a','b','c']
    # t1 = ('d','e','f')
    # s1 = {'g','h','i'}
    # print(str.join('-',l1))
    # print(str.join('+',t1))
    # print(str.join('*',s1))

    # a-b-c
    # d+e+f
    # h*g*i

    #==============================================================================================
    # str.isupper() 判断字符串中是否全部为大写,如果是则返回True,如果不是则返回False
    # s1 = 'AAAAAA'
    # s2 = 'aA'
    # print(s1.isupper())
    # print(s2.isupper())
    # True
    # False
    #==============================================================================================
    # str.title() 把字符串的每个单词的首字母全部大写,并赋值给另外一个变量

    # s1 = 'a sde aedd edeadf'
    # ret = s1.title()
    # print(ret)

    # A Sde Aedd Edeadf
    #==============================================================================================
    # str.split() 把一个字符串按照某个字符分割,然后把分割后的每个字符串放在一个list中
    # s1 = 'a/b/c'
    # s2 = 'a1cbda1cd'
    # print(s1.split('/'))
    # print(s2.split('1'))
    # ['a', 'b', 'c']
    # ['a', 'cbda', 'cd']

    #=============================================================================================
    # str.partition() 是根据某个字符把字符串分为三份,然后放在一个tuple中
    # s1 = 'a/b/c'
    # s2 = 'a1cbda1cd'
    # print(s1.partition('/'))
    # print(s2.partition('1'))

    # ('a', '/', 'b/c')
    # ('a', '1', 'cbda1cd')

    #==============================================================================================
    # str.strip() 如果strip不加任何参数,则会把字符串两边的空格全部去掉,也可以指定参数,去掉字符串两边的指定的字符
    # s1 = ' adb '
    # print(s1)
    # print(s1.strip())

    # adb
    # adb

    # s2 = 'aaBBBccccAAAaaaa'
    # print(s2)
    # print(s2.strip('a'))

    # aaBBBccccAAAaaaa
    # BBBccccAAA
    #==============================================================================================
    # str.startswith() 判断字符串是否以某个或者某几个字符开始
    # s1 = 'aaaacccccc'
    # print(s1.startswith('a'))
    # print(s1.startswith('c'))

    # True
    # False

    #==============================================================================================
    # str.upper() 把字符串全大写,并赋值给另外一个变量
    # s1 = 'abcde'
    # ret = s1.upper()
    # print(ret)

    # ABCDE
    #==============================================================================================
    # str.ljust() 用来填充字符串的,在字符串的右边添加a,直到新的字符串的长度到10个字符为止,并赋值给另外一个变量
    # s1 = 'abcde'
    # ret = s1.ljust(10,'a')
    # print(ret)

    # abcdeaaaaa
    #==============================================================================================
    # str.rjust() 用来填充字符串的,在字符串的左边添加a,直到新的字符串的长度到10个字符为止,并赋值给另外一个变量
    # s1 = 'addedd'
    # ret = s1.rjust(20,'&')
    # print(ret)

    # &&&&&&&&&&&&&&addedd

    #==============================================================================================
    替换字符串中的单个字符或者字符串
    ret = name1.replace('C','E')
    print(ret)

    ret = name1.replace('Ce','EF')
    print(ret)
    
    
    #==============================================================================================
    判断两个字符串是否相等
    name1 = 'Cevin'
    name2 = 'Cevin bob Cen'
    if name1.__eq__(name2):
    print("name1 is equal name2")



    #==============================================================================================
    大写变为小写,小写变为大写
    ret = name.swapcase()
    print(ret)


    #==============================================================================================
    填充,把tab【 】建替换为空格

    name = "Cev i n"
    ret = name.expandtabs()
    print(ret)
  • 相关阅读:
    数值的整数次方
    二进制中1的个数
    SpingBoot 启动自动给MongoDB建库
    Java 依赖冲突的解决办法
    Http协议
    你被限流了吗?
    LeetCode 783. 二叉搜索树节点最小距离
    Leetcode 687. 最长同值路径
    LeetCode 784. 字母大小写全排列
    LeetCode 面试题 08.06. 汉诺塔问题
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/6706871.html
Copyright © 2011-2022 走看看