字符串方法大全

目录

一、大小写转换

1 capitalize

2 casefold

3 lower

4 upper

5 swapcase

6 title

二、查找

1、count

2、endswith

3、find

4、rfind

5、index

6、rindex

三、替换

1、expandtabs

2、replace

四、分割

1、split

2、rsplit

3、splitlines

4、partition

5、rpartition

五、编码解码

五、判断类型的

1、isalnum

2、isalpha

3、isdecimal

4、isdigit

5、isidentifier

6、islower

7、isnumeric

8、isprintable

9、isspace

10 、istitle

11、isupper

12、startswith

六、格式化输出

1、format

2、format_map

七、截取、合并、填充

1、join

2、lstrip

3、strip

4、rstrip

5、ljust

6、rjust

7、zfill

八、映射表

1、maketrans

2、translate

九、其他方法

1、center

一、大小写转换

1、 capitalize

def capitalize_demo(): #首字母大写其他小写
    a="abblaHYCDsdj"
    print(a.capitalize())
    print(a)
capitalize_demo()


#输出结果  Abblahycdsdj

2 、casefold

def casefold_demo(): #将所有大写字符转换为小写
    a="jdsOYTHjlkA叁肆一"
    print(a.casefold())
casefold_demo()

#输出结果  jdsoythjlka叁肆一

 

3 、lower

def lower_demo(): #将所有大写字符转换为小写
    a = "abcABC"
    print(a.lower())
lower_demo()

#输出结果 abcabc

4、 upper

def upper_demo(): #将字符串转换为大写
    a = "kjdhaHHH"
    print(a.upper())
upper_demo()


#输出结果  KJDHAHHH

 

5 、swapcase

def swapcase_demo(): #转换字符串大小写
    a = "abcdefgHIJKLMN"
    print(a.swapcase())
swapcase_demo()

#输出结果 ABCDEFGhijklmn

6、 title

def title_demo(): #返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写
    a = "studentGrades"
    print(a.title())
title_demo()

#输出结果  Studentgrades

二、查找

1、count

def count_demo(): #返回字符在字符串里出现的次数
    a="abcdeabca"
    print(a.count("a",3,9)) #参数分别是待搜索字符,开始位置,结束位置
count_demo()

#输出结果  2

2、endswith

def endswith_demo(): #检查字符串是否以指定字符结尾
    a="abcdeabca"
    print(a.endswith("a")) #参数分别是待搜索字符,开始位置,结束位置
endswith_demo()

#输出结果  True

3、find

def find_demo():  #指定范围内查找字符串,若存在返回索引,若无返回-1
    a="kahduiqhfbsd"
    print(a.find("a",0,1)) #参数分别是待查找字符,开始位置,结束位置
find_demo()

#输出结果  -1

4、rfind

def rfind_demo(): #从右边查找字符,返回第一个找到的索引,若无返回-1
    a = "abdcdefg"
    print(a.rfind("d"))
rfind_demo()

#输出结果  4

5、index

def index_demo(): #查找字符串,若有返回索引,若无报错
    a = "abcdefg"
    print(a.index("c"))
    # print(a.index("k"))
    try:
        print(a.index("k"))
    except Exception as e:
        print(e)
index_demo()


#输出结果 
2
substring not found

6、rindex

def rindex_demo(): #从右边查找字符,返回第一个找到的索引,若无报错
    a = "abcdedfg"
    print(a.rindex("d"))
rindex_demo()

#输出结果 5

三、替换

1、expandtabs

def expandtabs_demo(): #将tab转换为空格
    a="ab       cdea bca"
    print(a.split(" "))
    b=a.expandtabs()
    print(b.split(" "))
expandtabs_demo()


输出结果
['ab', '', '', '', '', '', '', 'cdea', 'bca']
['ab', '', '', '', '', '', '', 'cdea', 'bca']

2、replace

def replace_demo(): #替换字符串 若num指定(第三个参数),则替换次数不超过num次
    a="abcdefgcccc"
    b=a.replace("c","H",3)
    print(b)
replace_demo()


输出结果
abHdefgHHcc

四、分割

1、split

def split_demo(): #以指定字符分割字符串,默认为空格
    a="abc dcefg hijkclmn"
    print(a.split())
    print(a.split("c",1)) #第二个参数为分割次数,默认为-1(分割所有)
split_demo()

输出结果
['abc', 'dcefg', 'hijkclmn']
['ab', ' dcefg hijkclmn']

2、rsplit

def rsplit_demo():  #分割字符串
    a = "abcdecfg"
    print(a.rsplit("c",1)) #第二个参数分割次数,默认-1(分割所有)
rsplit_demo()

#输出结果
['abcde', 'fg']

3、splitlines

def splitlines_demo(): #将每行元素分割形成一个列表,参数默认False,为True保留转义符
    a = " as d\nk kj\rks \r\n jsdkj"
    print(a.splitlines())
    print(a.splitlines(True))
splitlines_demo()


输出结果
[' as d', 'k kj', 'ks ', ' jsdkj']
[' as d\n', 'k kj\r', 'ks \r\n', ' jsdkj']

4、partition

def partition_demo():  #根据指定参数将字符分割为三部分,若指定字符串不存在则返回的第一个参数为原字符串,其他为空
    a = "abcdef"
    b=a.partition("c")
    c=a.partition("h")
    print(b,c)
partition_demo()

输出结果
('ab', 'c', 'def') ('abcdef', '', '')

5、rpartition

def rpartition_demo(): #根据指定参数从右边搜索分隔符,将字符分割为三部分,若指定字符串不存在则返回的第一个参数为原字符串,其他为空
    a = "abcdefg"
    print(a.rpartition("d"))
rpartition_demo()


输出结果
('abc', 'd', 'efg')

五、编码解码

def encode_decode_demo():
    a="abcdeabca"
    b=a.encode(encoding="utf-8") #编码
    print(b)
    c=b.decode(encoding="utf-8") #解码
    print(c)
encode_decode_demo()

输出结果
b'abcdeabca'
abcdeabca

五、判断类型的

1、isalnum

def isalnum_demo():#如果 string 至少有一个字符并且所有字符都是字母或数字或中文则返回 True,否则返回 False
    a = "<,"
    b="我"
    c="lsdj1234"
    print(a.isalnum())
    print(b.isalnum())
    print(c.isalnum())
isalnum_demo()


输出结果
False
True
True

2、isalpha

def isalpha_demo():#如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
    a = "123jasoijo"
    b = "jasoijo"
    print(a.isalpha())
    print(b.isalpha())
isalpha_demo()

输出结果
False
True

3、isdecimal

def isdecimal_demo(): #只包含十进制数字,若是返回True,否则返回False
    a="010101"
    print(a.isdecimal())
isdecimal_demo()

输出结果
True

4、isdigit

def isdigit_demo():#判断是否全是数字,若是返回True,否则返回False
    a="2324t"
    print(a.isdigit())
isdigit_demo()

输出结果
False

5、isidentifier

def isidentifier_demo(): #判断变量名是否合法(不包含内置变量)
    a="def"
    print(a.isidentifier())
isidentifier_demo()

输出结果
True

6、islower

def islower_demo():  #判断字母是否全是小写
    a="ejwoee"
    print(a.islower())
islower_demo()

输出结果
True

7、isnumeric

def isnumeric_demo():#判断是否全是数字,若是返回True,否则返回False
    a="23"
    print(a.isnumeric())
isnumeric_demo()


输出结果
True

8、isprintable

def isprintable_demo(): #若字符串都是可打印的或为空返回True 否则返回False
    a="jweoif\nwfr" #含有转义符
    b="skj"
    print(a.isprintable())
    print(b.isprintable())
isprintable_demo()


输出结果
False
True

9、isspace

def isspace_demo(): #判断字符串是否全是空格
    a="dso lj"
    b=" "
    print(a.isspace())
    print(b.isspace())
isspace_demo()

输出结果
False
True

10 、istitle

def istitle_demo(): #判断字符串是否是标题化的
    a="Demo"
    b="demo"
    print(a.istitle())
    print(b.istitle())
istitle_demo()


输出结果
True
False

11、isupper

def isupper_demo():  #字符串包含至少一个区分大小写的字符,且这些字符全部大写返回True 否则返回False
    a = "32134"
    b = "923uLheiu"
    c = "KKHK"
    print(a.isupper())
    print(b.isupper())
    print(c.isupper())
isupper_demo()


输出结果
False
False
True

12、startswith

def startswith_demo(): #检查指定范围内的字符是否是以指定的字符开头
    a = "abcdefg"
    print(a.startswith("c",2,6)) #参数分别为待查找字符,开始位置,结束位置
startswith_demo()


输出结果
True

六、格式化输出

1、format

def format_demo(): #格式化输出字符串
    name = "小邓"
    msg="吃饭了么?"
    print("Hello,%s,%s"%(name,msg))
    print(name,msg.format())
format_demo()

输出结果
Hello,小邓,吃饭了么?
小邓 吃饭了么?

2、format_map

def format_map_demo(): #格式化输出字符串
    name="小邓"
    print(name.format_map(name))
format_map_demo()

输出结果
小邓

七、截取、合并、填充

1、join

def join_demo(): #合并字符串
    a="$$"
    seq=["a","","b","c",""]
    print(a.join(seq))  #以a为分隔符合并seq中的元素
join_demo()

输出结果
a$$$$b$$c$$

2、lstrip

def lstrip_demo():  #截取掉首字符左边的空格
    a = "  a    bcd    abbb"
    b=a.lstrip()
    print(b,"a:",len(a),"b:",len(b))
lstrip_demo()


输出结果
a    bcd    abbb a: 18 b: 16

3、strip

def strip_demo(): #删除首尾的字符或字符序列(默认为空格或回车)
    a = "  oidsjjaso  "
    b = "  jdsoilossssss"
    print(a.strip())
    print(b.strip("s"))
strip_demo()


输出结果
oidsjjaso
  jdsoilo

4、rstrip

def rstrip_demo(): #删除字符串末尾指定字符,默认空格
    a = "  ab  cdefg  "
    b = "  abcd  efg"
    print(a.rstrip())
    print(b.rstrip("g"))
rstrip_demo()

输出结果
  ab  cdefg
  abcd  ef

5、ljust

def ljust_demo():  #返回一个原字符串,左对齐,右边用空格填充至指定长度
    a = "abc"
    b= a.ljust(10)
    print(b,len(b))
ljust_demo()


输出结果
abc        10

6、rjust

def rjust_demo(): #返回一个原字符串,右对齐,左边用空格填充至指定长度
    a = "abcdefg"
    print(a.rjust(10))
rjust_demo()


输出结果
   abcdefg

7、zfill

def zfill_demo(): #字符串右对齐,左边用0填充至指定长度
    a = "ldja"
    print(a.zfill(10))
zfill_demo()

输出结果
000000ldja

八、映射表

1、maketrans

def maketrans_demo():  #创建字符映射转换表,第一个参数是待转换字符,第二个参数是目标字符 返回的是ASCII码
    a = "abcdefg"     
    b = a.maketrans("h","H")
    print(a)
    print(b)
maketrans_demo()

输出结果
abcdefg
{104: 72}


def maketrans_demo():  #创建字符映射转换表,第一个参数是待转换字符,第二个参数是目标字符 返回的是ASCII码
    a = "abcdefg"       #暂时看不懂
    b = "1234567"
    table = a.maketrans(a,b)
    print(table)
maketrans_demo()

输出结果
{97: 49, 98: 50, 99: 51, 100: 52, 101: 53, 102: 54, 103: 55}

2、translate

def translate_demo():  #根据给出的表转换字符
    a = "abcdefg"
    b = "1234567"
    table = a.maketrans(a,b)
    print(table)
    str = "abcdefghhhhijklmn"
    print(str.translate(table))
translate_demo()


输出结果
{97: 49, 98: 50, 99: 51, 100: 52, 101: 53, 102: 54, 103: 55}
1234567hhhhijklmn

 

九、其他方法

1、center

def center_demo(): #返回字符串居中且两边用空格填充至指定的字符串长度
    a="jdsOYTHjlkA"
    print(a.center(50))
center_demo()


输出结果
                   jdsOYTHjlkA 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章