[PY3]——内置数据结构(3)——字符串及其常用操作
字符串及其常用操作xmind图

字符串的定义
1. 单引号/双引号
In [1]: s1='hello world'
In [2]: s1="hello world"
2. 三对单引号/三对双引号
In [8]: s1='''hello
...: world'''; print(s1) #三个单引号支持字符串分行
hello
world In [9]: s1="""hello world"""
转义
In [24]: s='I\'m jelly';print(s)
I'm jelly In [25]: s='file path:c:\\windows\\';print(s)
file path:c:\windows\ # 在三对单引号中,可以自由地使用单/双引号而不需要考虑转义引号
In [20]: sql='''select * table where name='jelly' ''';print(sql)
select * table where name='jelly' In [21]: sql='''select * table where name="jelly" ''';print(sql)
select * table where name="jelly" # r' ' 原样输出,不转义
In [27]: path=r'c:\windows\system32\???';print(path)
c:\windows\system32\???
访问字符串,字符串不可变
In [1]: print(s)
I'm jelly In [2]: print(s[0])
I In [3]: print(s[3]) In [4]: type(s[3])
Out[4]: str In [5]: s[3]='B'
TypeError: 'str' object does not support item assignment
字符串常用操作
###连接操作###
1. join()
# join是字符串的方法,参数是“内容为字符串”的“可迭代对象”,接收者是分隔符 In [7]: s=str()
In [8]: help(s.join)
join(...) method of builtins.str instance
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S. In [1]: lst=['I','\'m','jelly'] In [2]: print(lst)
['I', "'m", 'jelly'] In [3]: ' '.join(lst)
Out[3]: "I 'm jelly" In [4]: '/'.join(lst)
Out[4]: "I/'m/jelly" In [5]: lst //这个lst的内容是int而不是string所以抛出TypeError
Out[5]: [1, 2, 3, 4, 5, 6, 7, 8] In [6]: '.'.join(lst)
TypeError: sequence item 0: expected str instance, int found
###分割操作###
1. split()
In [13]: help(s.split)
split(...) method of builtins.str instance
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result. #split默认使用空格分割,并且多个空格会当成一个空格处理
In [17]: s1="I love python";s1.split()
Out[17]: ['I', 'love', 'python'] #应当注意若是split指定了分隔符为空格,则每个空格都处理
In [18]: s1="I love python";s1.split(' ')
Out[18]: ['I', '', 'love', '', 'python'] #seq参数表示指定分隔符,分隔符可以是任意字符串
In [21]: s2="I love python";s2.split('o')
Out[21]: ['I l', 've pyth', 'n'] #split从左往右分割,maxsplit参数表示分割次数,默认值为-1表示分割所有分隔符
In [25]: s3="i i i i i i"; s3.split(maxsplit=-1)
Out[25]: ['i', 'i', 'i', 'i', 'i', 'i'] In [26]: s3="i i i i i i"; s3.split(maxsplit=1)
Out[26]: ['i', 'i i i i i'] In [27]: s3="i i i i i i"; s3.split(maxsplit=2)
Out[27]: ['i', 'i', 'i i i i'] # 当字符串不含分隔符,以及当字符串只含分隔符的情况
In [11]: ''.split('=')
Out[11]: [''] In [12]: '='.split('=')
Out[12]: ['', '']
2. rsplit()
In [14]: help(s.rsplit)
rsplit(...) method of builtins.str instance
S.rsplit(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.
#rsplit与split的方向相反,是从右往左分割,但我们可以看到当不涉及maxsplit参数时,rsplit的结果split完全一致
#但split的效率高于rsplit
In [29]: s1="I love python";s1.rsplit()
Out[29]: ['I', 'love', 'python'] In [30]: s1="I love python";s1.rsplit(' ')
Out[30]: ['I', '', 'love', 'python'] In [31]: s2="I love python";s1.rsplit('o')
Out[31]: ['I l', 've pyth', 'n'] #涉及到maxsplit参数,则rsplit和split的输出结果相反
In [32]: s3="i i i i i i";s3.rsplit(maxsplit=-1)
Out[32]: ['i', 'i', 'i', 'i', 'i', 'i'] In [33]: s3="i i i i i i";s3.rsplit(maxsplit=1)
Out[33]: ['i i i i i', 'i'] In [34]: s3="i i i i i i";s3.rsplit(maxsplit=2)
Out[34]: ['i i i i', 'i', 'i']
3. splitlines()
In [15]: help(s.splitlines)
splitlines(...) method of builtins.str instance
S.splitlines([keepends]) -> list of strings
Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true. In [1]: s="""first line
...: second line"""
# splitlines按行分割,默认返回结果不带换行符
In [2]: s.splitlines()
Out[2]: ['first line', 'second line'] # 设置True参数则返回结果带换行符
In [3]: s.splitlines(True)
Out[3]: ['first line\n', 'second line']
4. partition()/rpartition()
In [4]: help(s.partition)
partition(...) method of builtins.str instance
S.partition(sep) -> (head, sep, tail)
Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it. If the separator is not
found, return S and two empty strings. # 将字符串按照传入的分隔符seq分割一次,返回结果总是一个三元组——(head,seq,tail) In [5]: s="first/second/third";s.partition('/')
Out[5]: ('first', '/', 'second/third') In [6]: s="first/second/third";s.rpartition('/')
Out[6]: ('first/second', '/', 'third') # partition常用于分割配置文件
In [8]: cfg='env=PATH=/usr/bin:$PATH'; cfg.partition('=')
Out[8]: ('env', '=', 'PATH=/usr/bin:$PATH') # 当字符串不含分隔符,以及当字符串只含分隔符的情况
In [9]: ''.partition('=')
Out[9]: ('', '', '') In [10]: '='.partition('=')
Out[10]: ('', '=', '') //总之输出的结果总是一个三元组
###字符的转化/排版类操作###
1. 大小写转化
# upper()转化为大写
In [1]: s='test';s.upper()
Out[1]: 'TEST' # lower()转化为小写
In [3]: s='Test';s.lower()
Out[3]: 'test' # title()将各单词首字母转化为大写
In [6]: s='i love python';s.title()
Out[6]: 'I Love Python' # capitalize()仅将首单词首字母转化为大写
In [7]: s='i love python';s.capitalize()
Out[7]: 'I love python' # casefold()通常用于忽略大小写
In [24]: s='Test TTest';s.casefold()
Out[24]: 'test ttest' # swapcase()大小写互换
In [27]: s='TEst';s.swapcase()
Out[27]: 'teST'
2. 排版相关(了解即可,用的不多)
# center()
In [9]: s='test';s.center(20)
Out[9]: ' test ' # zfill()
In [22]: s="";s.zfill(20)
Out[22]: '' # expandtabs(n)将table转化为n个空格
In [29]: '\t'.expandtabs(6)
Out[29]: ' '
###修改操作###
1. replace()
In [2]: help(s.replace)
replace(...) method of builtins.str instance
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced. # replace('old','new'),将字符串中的old全部替换成new
In [3]: s='red red green';s.replace('red','yellow')
Out[3]: 'yellow yellow green' # replace('old','new'count),count可以用来指定替换次数
In [4]: s='red red green';s.replace('red','yellow',1)
Out[4]: 'yellow red green'
2. strip()/lstrip()/rstrip()
In [5]: help(s.strip)
strip(...) method of builtins.str instance
S.strip([chars]) -> str
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead. In [6]: help(s.lstrip)
lstrip(...) method of builtins.str instance
S.lstrip([chars]) -> str
Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead. In [7]: help(s.rstrip)
rstrip(...) method of builtins.str instance
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead. # strip()用于移除字符串前后的空白
In [1]: s=' hhh hhh ';s.strip()
Out[1]: 'hhh hhh' In [3]: s='\n \r \t hhh hhh \t \n \r';s.strip()
Out[3]: 'hhh hhh' # strip()还可以移除指定的多个字符
In [4]: s='###hhh###kkk###';s.strip('#')
Out[4]: 'hhh###kkk' In [5]: s='{{ hhh }}';s.strip('{}')
Out[5]: ' hhh ' In [6]: s='{{ hhh }}';s.strip('{} ')
Out[6]: 'hhh' # lstrip()和rstrip()则分别为只移除左/右端
In [8]: s='{{ hhh }}';s.lstrip('{} ')
Out[8]: 'hhh }}' In [9]: s='{{ hhh }}';s.rstrip('{} ')
Out[9]: '{{ hhh'
2. center()/ljust()/rjust()
In [16]: help(s.center)
center(...) method of builtins.str instance
S.center(width[, fillchar]) -> str
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space) In [18]: help(s.ljust)
ljust(...) method of builtins.str instance
S.ljust(width[, fillchar]) -> str
Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space). In [19]: help(s.rjust)
rjust(...) method of builtins.str instance
S.rjust(width[, fillchar]) -> str
Return S right-justified in a string of length width. Padding is
done using the specified fill character (default is a space). In [9]: s='test';len(s)
Out[9]: 4 # rjust()填充字符串,原串在右侧(默认用空格填充,填充的字符可指定)
In [10]: s1=s.rjust(10,'#');print(s1);len(s1)
######test
Out[10]: 10 # ljust()填充字符串,原串在左侧
In [11]: s1=s.ljust(10,'#');print(s1);len(s1)
test######
Out[11]: 10 # center()填充字符串,原串在中间
In [13]: s1=s.center(11,'#');print(s1);len(s1)
####test###
Out[13]: 11 # 如果指定的填充字符宽度小于原字符串的宽度,则不做任何操作
In [14]: s1=s.center(3,'#');print(s1);len(s1)
test
Out[14]: 4
###查找操作###
1. find()
In [17]: help(s.find)
find(...) method of builtins.str instance
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure. # find()从左往右查找,找到第一个子串,返回子串首字母的索引
In [22]: s='holler hello love';s.find('hello')
Out[22]: 7 In [23]: s='holler hello love';s.find('h')
Out[23]: 0 # rfind()是从右往左查找
In [30]: s='holler hello love';s.rfind('h')
Out[30]: 7 # find同样可以指定索引范围
In [25]: s='holler hello love';s.find('h',3)
Out[25]: 7 # 查找不到则返回-1
In [24]: s='holler hello love';s.find('hhh')
Out[24]: -1
2. index()/rindex()
In [18]: help(s.index)
index(...) method of builtins.str instance
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found. # index()和find()用法一样
In [31]: s='holler hello love';s.index('hello')
Out[31]: 7 # 唯一的区别:是找不到子串时抛出ValueError异常
In [32]: s='holler hello love';s.index('hhh')
ValueError: substring not found
3. count()
In [20]: help(s.count)
count(...) method of builtins.str instance
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation. # count()用于统计字符串出现次数
In [33]: s='holler hello love';s.count('hello')
Out[33]: 1 In [35]: s='holler hello love';s.count('o')
Out[35]: 3 In [37]: s='holler hello love';s.count('o',0,6)
Out[37]: 1 # 找不到时抛出ValueError异常
In [36]: s='holler hello love';s.index('hhh')
ValueError: substring not found
###判断操作###
1. startswith()
In [39]: help(s.startswith)
startswith(...) method of builtins.str instance
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try. # startswith()用于判断是否以某个字符串开始,返回结果是bool
In [41]: s='holler hello love';s.startswith('h')
Out[41]: True In [42]: s='holler hello love';s.startswith('holler')
Out[42]: True In [43]: s='holler hello love';s.startswith('hhh')
Out[43]: False # 同样可以指定start和end表示索引从何开始、从何结束
In [44]: s='holler hello love';s.startswith('h',2,8)
Out[44]: False
2. endswith( )
In [45]: help(s.endswith)
endswith(...) method of builtins.str instance
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try. # endswith()用于判断是否以某个字符串结束,返回结果是bool
In [1]: s='holler hello love';s.endswith('e')
Out[1]: True In [2]: s='holler hello love';s.endswith('love')
Out[2]: True In [3]: s='holler hello love';s.endswith('o',2,12)
Out[3]: True
3. is*( ) string中is开头的方法用来做判断
# isalnum()判断字符串是否只含有字母或数字
# 可以用来判断是否有空格或其它字符
In [8]: s='holler';s.isalnum()
Out[8]: True In [9]: s='holler\t';s.isalnum()
Out[9]: False In [10]: s='holler hello love';s.isalnum()
Out[10]: False In [11]: s='holler123';s.isalnum()
Out[11]: True # isalpha()判断字符串是否只含有字母
In [13]: s='';s.isalpha()
Out[13]: False In [14]: s='abc';s.isalpha()
Out[14]: True In [15]: s='abc123';s.isalpha()
Out[15]: False # isdecimal()判断字符串是否只含有数字
In [19]: s='';s.isdecimal()
Out[19]: True In [20]: s='abc';s.isdecimal()
Out[20]: False In [21]: s='abc123';s.isdecimal()
Out[21]: False # isidentifier()判断字符是否是一个合法标识符
# 合法标识符:字母/下划线开头,仅包含字母、数字、下划线 In [22]: s='_abc';s.isidentifier()
Out[22]: True In [23]: s='1abc';s.isidentifier()
Out[23]: False In [24]: s='abc_123';s.isidentifier()
Out[24]: True
[PY3]——内置数据结构(3)——字符串及其常用操作的更多相关文章
- [PY3]——内置数据结构(6)——集合及其常用操作
集合及其常用操作Xmind图 集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...
- [PY3]——内置数据结构(7)——字典及其常用操作
字典及其常用操作Xmind图 关于字典 字典是一种key-value结构 字典是无序的 字典的定义 # {}大括号可以直接定义一个空字典 In [1]: d={};type(d) Out[1]: di ...
- [PY3]——内置数据结构(1)——列表及其常用操作
列表及其常用操作_xmind图 about列表 列表是一个序列,用于顺序存储数据 列表分为两种:ArrayList(用数组实现).LinkedList(用链表实现) 定义与初始化 #l ...
- [PY3]——内置数据结构(2)——元组及其常用操作
定义和初始化 #tuple() 使用工厂函数tuple定义一个空元组 #() 使用圆括号定义一个空元组 #(1,2,3) 使用圆括号定义有初始值的元组 #tuple(可迭代对象) 把可迭代对象转换为一 ...
- [PY3]——内置数据结构(5)——字符串编码
py2和py3中关于字符串的最大区别? python2中只有 unicode类型 而python3中有 string bytes两种类型 关于string和bytes的区分? 1.str是文本序列.b ...
- [PY3]——内置数据结构(4)——字符串格式化(format)
字符串格式化是拼接字符串的一种手段 join和+拼接字符串的方法,难以控制格式 printf style 字符串格式化 这种方法是从c语言继承过来的 # 待格式化的字符串:一个字符串存在占位符 In ...
- Python内置数据结构之字符串str
1. 数据结构回顾 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于字符串,但是字符串是不可变序列,因此所有的元素赋值和切片赋值都是非法的. >>> ...
- [PY3]——内置数据结构(9)——线性结构与切片/命名切片slice()
线性结构的总结 列表list 元组tuple 字符串str bytes bytearray的共同点: 都是顺序存储.顺序访问的: 都是可迭代对象: 都可以通过索引访问 线性结构的特征: 可迭代 ...
- [PY3]——内置数据结构(8)——解构与封装
### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...
随机推荐
- Buffer Pool--数据库内存结构
数据库从操作系统申请到的内存可分为两部分:1.缓存池内存(数据页和空闲页)2.非缓存池内存(线程/DLL/连接服务器等) Note:通过服务器实例属性设置的最大/最小服务器内存是指缓存池内存Note: ...
- Centos7下修复 视频播放器(先 安装VLC视频播放器)
用最新的CentOS7发现没有视频播放器,于是在http://pkgs.org/上查找,发现了nux dextop仓库上有, 于是到他的官网上http://li.nux.ro/repos.html查了 ...
- django 定制管理页面外观 模板文件不生效的解决方法
问题描述:大概过程跟下面描述的一样,简单来说就是照着例子学习的时候定制管理页面外观,按照文档要求拷贝了base_site.html文件到templates目录下,并且按照要求修改了settings.p ...
- roadflow作为工作流引擎服务中心webapi说明
将RoadFlow作为工作流引擎服务中心,其它第三方系统如OA,ERP等通过调用RoadFlow对外提供的标准WebApi接口来实现流程发送.退回.查询待办事项.已办事项.查看流转审批过程等操作.实现 ...
- python 单线程实现并发
单线程下支持并发(服务端): from gevent import spawn,monkey;monkey.patch_all() from socket import * def server(ip ...
- ElasticSearch.net NEST批量创建修改删除索引完整示例
本示例采用Elasticsearch+Nest 网上查了很多资料,发现用C#调用Elasticsearch搜索引擎的功能代码很分散,功能不完整,多半是非常简单的操作,没有成型的应用示例.比如新增或修改 ...
- [iOS笔试600题]一、语法篇(共有147题)
[A]1. @property 的作用是申明属性及真特性?[判断题] A.正确 B.错误 [A]2. @synthesize的作用是自动笠成属性的访问器(getter/setter)方法?[判断题] ...
- 爬虫实战4:用selenium爬取淘宝美食
方案1:一次性爬取全部淘宝美食信息 1. spider.py文件如下 __author__ = 'Administrator' from selenium import webdriver from ...
- jmeter 之 BeanShell PostProcessor跨线程全局变量使用
BeanShell PostProcessor是用户对一些变量的操作,操作方法很灵活,大概原理是通过parameters传回来对象,然后在script中对对象进行操作 场景:从登陆接口中获取token ...
- Spring Boot 之 Hello World 战记
1. Maven创建SpringBoot项目 2. application.properties spring.session.store-type=none // 解决默认的30+编译错误 3. d ...