资讯动态

Python 中字符串常用方法

发布时间:2026/10/27 18:57:18 来源:尧图企业网站定制
一、大小写转换方法说明示例upper()转大写abc.upper()→ABClower()转小写ABC.lower()→abccapitalize()首字母大写其余小写hello world.capitalize()→Hello worldtitle()每个单词首字母大写hello world.title()→Hello Worldswapcase()大小写互换Hello.swapcase()→hELLOs hello world print(s.upper()) # HELLO WORLD print(s.lower()) # hello world print(s.capitalize()) # Hello world print(s.title()) # Hello World二、查找与判断方法说明示例find(sub)查找子串找不到返回-1hello.find(l)→2index(sub)查找子串找不到抛异常hello.index(l)→2rfind(sub)从右查找hello.rfind(l)→3count(sub)统计子串出现次数hello.count(l)→2startswith(prefix)是否以某前缀开头hello.startswith(he)→Trueendswith(suffix)是否以某后缀结尾hello.endswith(lo)→Truein运算符是否包含子串l in hello→Trues hello world print(s.find(world)) # 6 print(s.find(python)) # -1 print(s.count(l)) # 3 print(s.startswith(hello)) # True print(s.endswith(world)) # True说明find()vsindex()找不到时find返回-1index抛ValueError。三、去除空白/字符方法说明示例strip()去除两端空白 hi .strip()→hilstrip()去除左侧空白 hi .lstrip()→hi rstrip()去除右侧空白 hi .rstrip()→ histrip(chars)去除两端指定字符xxhixx.strip(x)→his hello print(s.strip()) # hello print(s.lstrip()) # hello print(s.rstrip()) # hello # 去除指定字符 print(---hello---.strip(-)) # hello四、分割与连接方法说明示例split(sep)按分隔符分割成列表a,b,c.split(,)→[a, b, c]rsplit(sep)从右分割a,b,c.rsplit(,, 1)→[a,b, c]splitlines()按行分割a\nb.splitlines()→[a, b]join(iterable)用字符串连接列表-.join([a, b])→a-bpartition(sep)分成三部分前、分隔符、后ab.partition()→(a, , b)s apple,banana,orange print(s.split(,)) # [apple, banana, orange] print(s.split(,, 1)) # [apple, banana,orange] # join 是 split 的逆操作 words [apple, banana] print(-.join(words)) # apple-banana说明split()不传参数时按任意空白字符分割并自动去除多余空白。s hello world print(s.replace(world, python)) # hello python print(s.replace(l, L, 1)) # heLlo world # 3.9 print(hello.removeprefix(he)) # llo print(hello.removesuffix(lo)) # hel六、格式化方法说明示例format()格式化字符串{} {}.format(a, b)f-string字面量格式化推荐f{name} {age}center(width)居中对齐hi.center(6, *)→**hi**ljust(width)左对齐hi.ljust(6, *)→hi****rjust(width)右对齐hi.rjust(6, *)→****hizfill(width)左侧补零42.zfill(5)→00042# f-string推荐 name, age Alice, 25 print(f{name} is {age} years old) # format print({} is {} years old.format(name, age)) # 对齐 print(hi.center(10, -)) # ----hi---- print(hi.ljust(10, -)) # hi-------- print(hi.rjust(10, -)) # --------hi print(42.zfill(5)) # 00042七、类型判断方法说明示例isalpha()是否全是字母abc.isalpha()→Trueisdigit()是否全是数字123.isdigit()→Trueisalnum()是否全是字母或数字ab1.isalnum()→Trueisspace()是否全是空白 .isspace()→Trueisupper()是否全是大写ABC.isupper()→Trueislower()是否全是小写abc.islower()→Trueistitle()是否是标题格式Hello World.istitle()→Trueisnumeric()是否是数字含中文数字一二三.isnumeric()→Trueisidentifier()是否是合法标识符my_var.isidentifier()→Trueprint(abc.isalpha()) # True print(123.isdigit()) # True print(ab12.isalnum()) # True print( .isspace()) # True print(ABC.isupper()) # True print(Hello World.istitle()) # True八、编码与转换方法说明示例encode(encoding)字符串转字节中文.encode(utf-8)decode()字节转字符串在 bytes 上调用bhi.decode()s 你好 b s.encode(utf-8) print(b) # b\xe4\xbd\xa0\xe5\xa5\xbd print(b.decode(utf-8)) # 你好九、其他实用方法方法说明示例len(s)字符串长度内置函数len(hello)→5s[i]索引访问hello[0]→hs[a:b]切片hello[1:3]→els[::-1]反转hello[::-1]→ollehord(c)字符转 ASCII 码ord(A)→65chr(n)ASCII 码转字符chr(65)→As hello print(len(s)) # 5 print(s[0]) # h print(s[-1]) # o print(s[1:3]) # el print(s[::-1]) # olleh print(ord(A)) # 65 print(chr(65)) # A

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑