(原创)Python字符串系列(1)——str对象
在本博客 《Python字符串系列》 中,将介绍以下内容:
本文将介绍Python内置的 str 类型,列举Python中字符串对象支持的方法,使用这些方法可以实现强大的字符串处理功能。
在Python 2 中,普通字符串与Unicode字符串有着明确的区分,二者都是Python内置的基本类型,例如:
1 2 3 4 | >>> type ( str ) < type 'type' > >>> type ( unicode ) < type 'type' > |
str 类型的字符串通常被称为普通字符串(plain string),区别于 unicode 类型的字符串(unicode string):
1 2 3 4 5 6 | >>> s = 'this is a plain string.' >>> type (s) < type 'str' > >>> u = u 'this is a unicode string.' >>> type (u) < type 'unicode' > |
unicode字符串通常以 'u' 开头。
在Python 2中,使用 抽象基类 basestring 判断一个字符串是不是 str 类型或者 unicode 类型的实例,因为二者都是 basestring 的子类。
1 2 3 4 5 6 | >>> issubclass ( str , basestring ) True >>> issubclass ( unicode , basestring ) True >>> issubclass ( unicode , str ) False |
本文将介绍Python字符串的内置方法,这些方法对于 plain string 和 unicode 字符串同样适用,如果执行操作的s是一个plain string,那么返回的字符串也是一个plain string,unicode类似。后续的文章将详细分析 unicode 字符串的特性及编码上的一些特点。
Python中,字符串是不可变的序列,支持:重复、连接、索引和切片等操作,例如:
1 2 3 4 | >>> s * n # 将 s 重复n次 >>> s1 + s2 # 将字符串 s1 和 s2 连接 >>> s[ 0 ] # 索引字符串 s1 中的第一个字符 >>> s[ 0 : 2 ] # 返回字符串 s 中的前两个字符 |
这些操作都不会改变参与运算的字符串,除非进行显式地赋值。此外,Python还包括了许多字符串处理的小技巧,如:
- 使用s[::-1]可以翻转整个字符串
- 如果一个字符串全部由数字组成,而开头有0,则使用 int(s) 能够自动除去开头的0,将原来的字符串转成一个有意义的整数。
Python内置了丰富的字符串处理功能
1. 首字母大写
capitalize()
s.capitalize() |
返回s的一份拷贝,并不改变s。如果 s 的首字符是一个字母,则拷贝的首字母将其改成大写,其余所有字母转成小写。
例如:
1 2 3 4 5 6 | >>> 'this is a test string.' .capitalize() 'This is a test string.' >>> '_this is a test string.' .capitalize() # 开头不是字母,不变 '_this is a test string.' >>> 'this is A test string.' .capitalize() # 除开头外的其他位置上的字母全转成小写 'This is a test string.' |
2. 对齐方式
(1)左右对齐 ljust()、rjust()
s.ljust(width[, fillchar])<br>s.rjust(width[, fillchar]) |
返回一个长度为 max(len(s), width) 的字符串,如果 width > len(s),则左/右对齐,并在另一端填充 fillchar
例如:
1 2 3 4 5 6 | >>> '1234' .rjust( 8 , '#' ) '####1234' >>> '1234' .ljust( 8 , '#' ) '1234####' >>> '1234' .ljust( 2 , '#' ) '1234' |
(2)居中 center()
s.center(n, fillchar = ' ' ) |
返回一个新的字符串,新字符串的长度为 max(len(s), n),当 n > len(s)时,使用参数 fillchar (默认为空格)填充新字符串中其余的位置,并将 s 置于新字符串的中部。
例如:
1 2 3 4 5 6 7 8 | >>> 'test' .center( 3 ) 'test' >>> 'test' .center( 5 ) ' test' >>> 'test' .center( 6 , '#' ) '#test#' >>> 'test' .center( 7 , '~' ) '~~test~' |
可见当左右无法均衡填充时,优先填充左侧。
3. 计数
count()
s.count(sub, start = 0 , end = sys.maxint) |
统计 s[start:end] 中,子串 sub 出现的次数。
4. str 与 unicode 的转换
(1)str到unicode——decode()
1 | S.decode([encoding[,errors]]) |
使用 decode() 函数可以将 str 类型的plain string 转换成 unicode 类型的字符串,
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> s = '你好' >>> u = s.decode( 'gbk' ) >>> type (s) < type 'str' > >>> type (u) < type 'unicode' > >>> print s 你好 >>> print u 你好 >>> s '\xc4\xe3\xba\xc3' >>> u u '\u4f60\u597d' >>> len (s) 4 >>> len (u) 2 |
(2)Unicode 到 str——encode()
S.encode([encoding[,errors]]) |
使用encode()则可以将 unicode 字符串 转换成 str 类型的 plain string。
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> u = u '你好' >>> s = u.encode( 'gbk' ) >>> type (u) < type 'unicode' > >>> type (s) < type 'str' > >>> u u '\u4f60\u597d' >>> s '\xc4\xe3\xba\xc3' >>> print u 你好 >>> print s 你好 >>> len (u) 2 >>> len (s) 4 |
5. 前后缀
endswith()、startswith()
1 | S.endswith(suffix[, start[, end]])<br>s.startswith(prefix[, start[, end]]) |
返回 bool 型结果,
判断 s[start:end]是否以 suffix 结尾。
6. 扩展制表符
expandtabs()
1 | S.expandtabs([tabsize]) |
tabsize默认为8,返回一个 s 的拷贝,其中的“\t”都被扩展成 tabsize 个空格。
例如:
1 2 | >>> 'test\ttest' .expandtabs() 'test test' |
7. 定位
(1)定位不到时返回 -1 find()、rfind()
1 | s.find(sub [,start [,end]])<br>s.rfind(sub [,start [,end]]) |
返回 int 型结果,表示 sub 在 s[start:end] 中第一次出现的下标。如果在 s[start:end] 中没有找到子串 sub,则返回 -1。
例如:
1 2 3 4 5 6 7 8 | >>> 'testtest' .find( 'est' ) 1 >>> 'testtest' .find( 'tt' ) 3 >>> 'testtest' .find( 'tt' , 3 ) 3 >>> 'testtest' .find( 'tt' , 4 ) - 1 |
(2)定位不到时抛出异常 index()、rindex()
1 | S.index(sub [,start [,end]])<br>s.rindex(sub [,start [,end]]) |
功能与 find() 类似,但是如果没有找到 sub ,则抛出 ValueError。
例如:
1 2 3 4 | >>> 'hello this world' .index( '$' ) Traceback (most recent call last): File "<stdin>" , line 1 , in <module> ValueError: substring not found |
8.
format()
1 | S. format ( * args, * * kwargs) |
返回 字符串 型结果,
9. 内容形式判断
isalnum()、isalpha()、isdigit()、isspace()
1 | s.isalnum() |
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 和 数字 组成。
1 | s.isalpha() |
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 组成。
1 | s.isdigit() |
返回布尔型结果,
判断 s 是不是非空,且全部由 数字 字符组成。
例如:
1 2 3 4 | >>> '123' .isdigit() True >>> '123.456' .isdigit() False |
1 | s.isspace() |
如果 len(s) > 0,且其中的所有字符都是空格,则返回True;
如果 s 为空,或s中存在至少一个非空格的字符,则返回False。
10. 大小写
(1)小写 islower()、lower()
1 | s.islower()<br>s.lower() |
返回布尔型结果,
如果 s 中不含一个小写字母,或至少含有一个大写字母,则返回False,否则返回True,包含其他字符并不影响。
例如:
1 2 3 4 5 6 7 8 9 10 11 12 | >>> '123.456' .islower() False >>> 'abcde' .islower() True >>> 'abcde$' .islower() True >>> 'abcde#%^%' .islower() True >>> 'abcdeF' .islower() False >>> 'a.213214$#@^%$@' .islower() True |
(2)大写 isupper()、upper()
1 | s.isupper()<br>s.upper() |
如果 s 中包含的所有字母都是大写,则返回 True
如果s 中不包含字母,或者至少包含一个小写字母,则返回False。
例:
1 2 3 4 5 6 | >>> 'ABC$@' .isupper() True >>> 'ASDFGq' .isupper() False >>> ''.isupper() False |
(3)交换大小写 swapcase()
s.swapcase() |
11. "titlecase"
istitle()、title()
1 | s.istitle()<br>s.title() |
判断一个字符串是不是“titlecase”:每个独立的连续字母段都以大写字母开头。
例如:
1 2 3 4 5 6 7 8 | >>> 'A Title' .istitle() True >>> 'a Title' .istitle() False >>> '123 this is a string' .istitle() False >>> 'This Is a String' .istitle() False |
12. 连接
join()
s.join(iterable) |
以 s 为分隔符连接 iterable 中的字符串
例如:
1 2 | >>> '$' .join([ 'hello' , 'this' , 'world' ]) 'hello$this$world' |
13. 拆分
(1)保留分隔符的一次拆分 partition()、rpartition()
s.partition(sep)<br>s.rpartition(sep) |
以 sep 为分隔符拆分 s ,返回 (head, sep, tail) 形式的三元组。
例如:
1 2 3 4 | >>> 'hello$this$world' .partition( '$' ) ( 'hello' , '$' , 'this$world' ) >>> 'hello$this$world' .rpartition( '$' ) ( 'hello$this' , '$' , 'world' )<br>>>> 'hello this world' .partition( '$' )<br>(' ', ' ', ' hello this world') |
(2)不保留分隔符的完全拆分 split()、rsplit()、splitlines()
s.split([chars])<br>s.rsplit([sep [,maxsplit]])<br>s.splitlines(keepends = False ) |
例如:
1 2 | >>> 'hello$this$world' .split( '$' ) [ 'hello' , 'this' , 'world' ] |
14.
lstrip()、strip()、rstrip()
1 2 3 | s.lstrip([chars]) #从开头删除 s.strip([chars]) # 左右两端同时删除 s.rstrip([chars]) # 从结尾删除 |
16. 替换
replace()
s.replace(old, new[, count]) |
18.
translate()
s.translate(table [,deletechars]) |
s.zfill(width) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· DeepSeek智能编程
· 精选4款基于.NET开源、功能强大的通讯调试工具
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?