字符串及其常用操作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)——字符串及其常用操作的更多相关文章

  1. [PY3]——内置数据结构(6)——集合及其常用操作

    集合及其常用操作Xmind图          集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...

  2. [PY3]——内置数据结构(7)——字典及其常用操作

    字典及其常用操作Xmind图 关于字典 字典是一种key-value结构 字典是无序的 字典的定义 # {}大括号可以直接定义一个空字典 In [1]: d={};type(d) Out[1]: di ...

  3. [PY3]——内置数据结构(1)——列表及其常用操作

    列表及其常用操作_xmind图         about列表 列表是一个序列,用于顺序存储数据 列表分为两种:ArrayList(用数组实现).LinkedList(用链表实现) 定义与初始化 #l ...

  4. [PY3]——内置数据结构(2)——元组及其常用操作

    定义和初始化 #tuple() 使用工厂函数tuple定义一个空元组 #() 使用圆括号定义一个空元组 #(1,2,3) 使用圆括号定义有初始值的元组 #tuple(可迭代对象) 把可迭代对象转换为一 ...

  5. [PY3]——内置数据结构(5)——字符串编码

    py2和py3中关于字符串的最大区别? python2中只有 unicode类型 而python3中有 string bytes两种类型 关于string和bytes的区分? 1.str是文本序列.b ...

  6. [PY3]——内置数据结构(4)——字符串格式化(format)

    字符串格式化是拼接字符串的一种手段 join和+拼接字符串的方法,难以控制格式 printf style 字符串格式化 这种方法是从c语言继承过来的 # 待格式化的字符串:一个字符串存在占位符 In ...

  7. Python内置数据结构之字符串str

    1. 数据结构回顾 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于字符串,但是字符串是不可变序列,因此所有的元素赋值和切片赋值都是非法的. >>> ...

  8. [PY3]——内置数据结构(9)——线性结构与切片/命名切片slice()

    线性结构的总结 列表list  元组tuple  字符串str  bytes  bytearray的共同点: 都是顺序存储.顺序访问的: 都是可迭代对象: 都可以通过索引访问 线性结构的特征: 可迭代 ...

  9. [PY3]——内置数据结构(8)——解构与封装

    ### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...

随机推荐

  1. | 与|| ,& 与&&

    & 既是位运算符又是逻辑运算符,&的两侧可以是int,也可以是boolean表达式 举例:12&5 的值是多少?答:12转成二进制数是1100(前四位省略了),5转成二进制数是 ...

  2. 那些令人敬佩的刚学OI的大佬

    我是萌新刚学OI,请问LCT怎么写常树最小啊 我是女生刚学OI,请问树链剖分哪里写挂了? 萌新求教,这棵SBT哪里有问题啊啊啊…… 刚学OI,请问可持久化非确定状态AC自动分块维护线段平衡仙人掌优化最 ...

  3. FutureTask与Fork/Join

    在学习多线程的过程中,我们形成了一种思维习惯.那就是对于某个耗时操作不再做同步操作,让他分裂成一个线程之后执行下一步,而线程执行耗时操作.并且我们希望在我们需要它返回的时候再去调用它的结果集.好比我们 ...

  4. select子句排列顺序与聚集函数

    selcet   要返回的列或表达式 from   从中检索数据的表 where    行级过滤 group by 分组说明 having 组级过滤 order by  输出排列顺序   ASC正序排 ...

  5. python --爬虫基础 --爬取今日头条 使用 requests 库的基本操作, Ajax

    '''思路一: 由于是Ajax的网页,需要先往下划几下看看XHR的内容变化二:分析js中的代码内容三:获取一页中的内容四:获取图片五:保存在本地 使用的库1. requests 网页获取库 2.fro ...

  6. docker微服务部署之:二、搭建文章微服务项目

    docker微服务部署之:一,搭建Eureka微服务项目 一.新增demo_article模块,并编写代码 右键demo_parent->new->Module->Maven,选择M ...

  7. STM32-RS232通信软硬件实现

    OS:Windows 64 Development kit:MDK5.14 IDE:UV4 MCU:STM32F103C8T6/VET6 AD:Altium Designer 18.0.12 1.RS ...

  8. ownCloud问题处理server replied 423 Locked to

    打开owncloud 数据库备份:oc_file_locks表(备份免错哦)然后清空该表,客户端同步一次,故障解决 owncloud大的数据无法同步..

  9. CSS04--对齐、 布局、导航栏

    我们接着上一章,继续学习一些有关对齐.布局.导航栏的内容. 1.水平块对齐:    1.1 margin:把左和右外边距设置为 auto,规定的是均等地分配可用的外边距.结果就是居中的元素    .c ...

  10. C#接口实现多态

    我比较喜欢对感兴趣的理论进行反复的理解甚至理解背诵下来,接下来再复习一下什么叫多态(哈哈哈) 多态:在同一粒度视图下对相同类型的事物不做区别的统一操作 接下来看一下接口和引擎类是如何实现多态的: 一. ...