01-Python 中的数据类型-02-字符串类型

总体 要讲的大纲内容 如下

  • 数字类型- int float complex
  • 字符串类型 Text Sequence Type- str
  • 序列类型 - list tuple range
  • 集合类型 - set frozenset
  • 上下文管理器类型 - 比较复杂暂时 不用掌握
  • 文本序列类型
  • 二进制序列类型 bytes bytesarray memoryview
  • 真值检测
  • 比较运算符
  • 逻辑运算符
  • 如何判断 一个对象 是什么类型- type(xxx)

今天 继续讲 基础类型的字符串类型 , 字符串类型是比较常见的。

生活中 东西的名称 ,苹果 对应就是 Apple ,橘子 orange

字符串的定义

在Python中 字符串 是如何定义的呢?

一般来说 有三种方式定义一个字符串 。 单引号, 双引号,和三单引号,三双引号

注意 这里 单引号,双引号,都是英文字符, 不是中文字符。

name = 'frank'

name2 = "frank2"

name3 = '''frank'''

name4 = """frank4"""

print(name, type(name))
print(name2, type(name2))
print(name3, type(name3))
print(name4, type(name4))

img01

这种几种方式 几乎没有区别。有时候 可以替换。

假设你的字符串有' 这个时候,就可以用双引号。

>>> sentence="Frank's book is here."
>>> sentence
"Frank's book is here."

>>> type(sentence)
<class 'str'>

还有你的字符串比较长,一行很难放下,有多行的时候。

这个时候可以用三引号

sentence="""The debugger caught an exception in your WSGI application.
You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, 
you can click on the "Traceback" headline. 
From the text traceback you can also create a paste of it. 
For code execution mouse-over the frame you want to debug and click on the console icon on the right side."""

>>> sentence="""The debugger caught an exception in your WSGI application.
... You can now look at the traceback which led to the error.
... To switch between the interactive traceback and the plaintext one, 
... you can click on the "Traceback" headline. 
... From the text traceback you can also create a paste of it. 
... For code execution mouse-over the frame you want to debug and click on the console icon on the right side."""
>>> 

字符串 常见的方法(操作)

capitalize ,title

str.capitalize() 这个方法 返回一个字符串 , 这个字符串相对于str 开头第一个字符大写

str.title() 每个单词的首字母 大写

看下面例子

>>> s = 'frank  aaa  bbb'
>>> s.capitalize()
'Frank  aaa  bbb'
>>> s.title()
'Frank  Aaa  Bbb'

encoding

str.encoding() 字符串可以进行编码,默认utf-8 编码,简单理解 字符串可以转换不同的编码格式 。utf-8用的比较多.

>>> name = 'frank'
>>> 
>>> name.encode(encoding="utf-8")
b'frank'
>>> name2= name.encode(encoding="utf-8")
>>> type(name2)
<class 'bytes'>

这里 发现 经过 str.encode 把 一个str 类型 转换成了一个 btyes 类型。bytes 类型 也是python的基础数据类型,之后会说到 。

现在要记得 str 如何转成bytes 类型 使用 encode 这个方法 。

位置概念

补充一点:

str 这个 数据类型可以有"位置" 的概念 ,默认第一个位置 是从0 开始的。

>>> name='frank'
>>> name[0]
'f'
>>> name[1]
'r'
>>> name[4]
'k'
>>> name[3]
'n'

从上面可以看出第0 号位置是’f’ , 第四号位置是 ‘k’ .

注意 对应 frank 这个字符串 ,最大的位置是4,能不能取5呢? 肯定不能,因为位置5 已经超出了 frank 的范围。看下面的例子

>>> name[5]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: string index out of range

有的时候 我并不获取一个字符,而是获取 一段 , python中一个东西 叫切片 使用 方法 str[a:b] , a<=x<b 这个范围

>>> sentence
'The debugger caught an exception in your WSGI application.'

>>> sentence[0:3]
'The'
>>> sentence[4:12]
'debugger'

find,rfind

str.find() 寻找一个字符串 是否在 str 中 ,如果在 返回对应最小的位置的下标(位置),如果没有找到 返回 -1

>>> sentence="""The debugger caught an exception in your WSGI application."""
>>> sentence
'The debugger caught an exception in your WSGI application.'
>>> sentence.find('an')
20
>>> sentence.find('the')
-1
>>> sentence.find('Frank')
-1
>>> sentence.find('T')
0
>>> sentence.find('debug')
4



str.rfind() 寻找一个字符串 是否在 str 中 ,如果在 返回对应最大的位置的下标(位置),如果没有找到 返回 -1

>>> # 再来看一个例子
>>> sentence ='I like swimming and I like sports'
>>> sentence.find('like')
2
>>> sentence[2]
'l'
>>> sentence.rfind('like')
22
>>> sentence[2:6]
'like'
>>> sentence[22:26]
'like'

index,rindex

str.index 这个方法 和find 相似,唯一的区别是 当寻找的子串没有找到的时候会报错,抛出 ValueError的异常。

>>> sentence
'The debugger caught an exception in your WSGI application.'
>>> sentence.index('an')
20
>>> sentence.index('in')
33
>>> sentence.index('on')
30
>>> sentence.index('over')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: substring not found

str.rindex() 和 str.find() 方法类似, 唯一的区别是 没有找到的情况下,也会抛出一个 ValueError的异常

startswith ,endswith

str.startswith(prefix) 判断 字符串 是否是prefix开头,返回 True ,False

str.endswith(suffix) 判断 字符串 是否是suffx 结尾返回 True ,False

>>> s ='apple  aaa bbb ccc  dd'
>>> s.startswith('a')
True
>>> s.startswith('app')
True
>>> s.startswith('apps')
False
>>> s.endswith('dd')
True
>>> s.endswith('d')
True
>>> s.endswith('df')
False

len 获取字符串长度

len(s) 获取字符串的长度

>>> s
'apple  aaa bbb ccc  dd'
>>> len(s)
22

replace

str.replace(old, new[, count]) 返回一个 将old 替换为new 的一个字符串 ,如果 count 给定了值,只替换 count 次。如果没有 给定count 就是全部替换。

>>> # 把 A 替换成 a 
>>> string ='aaaaAaaaAaaaAAa'
>>> string.replace('A','a')
'aaaaaaaaaaaaaaa'
>>> # 最多替换2次
>>> string.replace('A','a',2)
'aaaaaaaaaaaaAAa'

strip,lstrip,rstrip

有时候 一个字符串 没有那么规整,比如前后都有空格,你想去掉 前面,或者的空格 保留中间的部分 .

str.lstrip() 去掉 左边空格,返回一个字符串

str.rstrip() 去掉 右边空格,返回一个字符串

str.strip() 去掉 两边 边空格,返回一个字符串

>>> s ='       aaa bbb ccc    '
>>> s
'       aaa bbb ccc    '
>>> s.lstrip()
'aaa bbb ccc    '
>>> s
'       aaa bbb ccc    '
>>> s.rstrip()
'       aaa bbb ccc'
>>> s.strip()
'aaa bbb ccc'

upper,lower

大小写 转换

str.upper() 转换为 大写,返回一个字符串

str.lower() 转换为小写,返回一个字符串

>>> 'abc'.upper()
'ABC'
>>> 
>>> 'Frank'.upper()
'FRANK'


>>> 'FRANK'.lower()
'frank'
>>> 'FrAnk'.lower()
'frank'

str.islower()

str.isupper()

判断是不是全是 大写 或者小写,返回 布尔值True, False

>>> 'frank'.islower()
True
>>> 'frank'.isupper()
False
>>> 'FRANK'.isupper()
True

swapcase

str.swapcase() 这个方法 有点意思 ,就是改变字符的大小写。

原本大写字符 -> 小写字符

原本小写字符 变成 大写字符

>>> 'aaBBccDD'.swapcase()
'AAbbCCdd'

+ 连接字符串

连接两个字符串 用 +

>>> name ='frank'
>>> hobby = 'swimming'
>>> verb = 'likes'
>>> name + verb + hobby
'franklikesswimming'
>>> # 好丑,重新连接
>>> name +' '+ verb+' ' + hobby +'.'
'frank likes swimming.'

join

str.join(iterable) 返回一个字符串,该字符串是可迭代的字符串的串联,有点抽象,举个例子

>>> hello ='hello'
>>> world='world'


>>> ','.join(world)
'w,o,r,l,d'
>>> hello.join(world)
'whelloohellorhellolhellod'

比如用, join 一个字符串,就是 用逗号将world 每一个字符连接起来 。

hello.join (world) 就是用 hello 把 world 每一个字符 连接起来

‘whelloohellorhellolhellod’ 就是下面的样子 。

比如用加号 把 hello 连接起来

>>> '+'.join(hello)
'h+e+l+l+o'

count

str.count(sub [, start[, end]]) 在 [start,end ] 范围内 寻找 没有子串sub ,如果有的话,出现的次数。如果没有返回0 , start ,end 如果不指定的话,默认 搜索整个字符串的范围 ,即 start=0,end =len(str)-1

注意: str 索引 是从0 开始的。

>>> sentence ="hello hello world world hello hello"
>>> sentence.count('hello')
4
>>> sentence.count('aaa')
0


>>> sentence[0:17]
'hello hello world'
>>> sentence.count('hello',0,17)
2

split ,splitlines

str.split()

str.split(sep=None, maxsplit=-1) 分开的意思 拆分字符串,

参数sep 就是要分隔字符的标识,maxsplit 拆分的次数,默认值 是-1,就是尽可能大的次数拆分这个字符串。

>>> #用逗号拆分
>>> 'one,two,three,four'.split(',')
['one', 'two', 'three', 'four']
>>> nums='one,two,three,four'.split(',')
>>> nums
['one', 'two', 'three', 'four']
>>> type(nums)
<class 'list'>



>>> 'one,two,three,four'.split(',',maxsplit=2)
['one', 'two', 'three,four']

首先 就是用逗号拆分这个字符串,发现这个字符串全部通过逗号拆开了, 并且 这些值 放到了[ ] 里面,

通过 type 查看这个 类型 发现是 list, 现在 你又发现了一种 数据类型 叫list。 它可以保存一系列的数据。

maxsplit 设置 切分次数。 上面的例子 设置2 ,那么之后的字符串就单独 放在一个一起了。

splitlines 这个是以 ‘\n’ 作为换行符 ,并且返回一个list ,但是 如果 这个字符以最后一个\n 结尾,

两个 方法 稍微有点区别,如果使用split(’\n’)会被拆成两条数据。 而 splitlines 只会是一条数据, 这就是有点区别的地方 。

>>> 'aaa\nbbb\nccc\nddd'.splitlines()
['aaa', 'bbb', 'ccc', 'ddd']

>>> 'aaa\nbbb\nccc\nddd'.split('\n')
['aaa', 'bbb', 'ccc', 'ddd']


>>> 'one line\n'.split('\n')
['one line', '']
>>> 'one line\n'.splitlines()
['one line']

isxxxx

isxxx 系列 判断 是不是某些 特殊的值, 返回 True ,False

这些 平常用到不是特别多,但是用到的时候,只要去查一些 文档就好了。

str.isascii() 是不是ASCII 吗?
str.isalnum() 所有的字符是否都是 数字
str.isalpha() 所有的字符是不是都是 字母
str.isdecimal() 所有的字符是不是都是 小数的字符
str.isidentifier() 所有的字符 都是标识符
str.isspace() 所有的字符 是不是 都是 空格
str.isprintable() 所有的字符 是不是都是 可以打印的

name = '1.343'
name.isascii()
name.isalnum()
name.isalpha()
name.isdecimal()
name.isidentifier()
name.isspace()
name.isprintable()

总结

​ 今天 主要讲了字符串的表示,以及常用方法,查找,拼接,替换,统计,大小写转换等。

这里可能 你不能把所有的方法都能记住, 但是 用到的时候你知道如何查文档就可以了。还有今天接触了两种数据类型, 一种是bytes 类型, 一种是 list 类型 ,还记得 他们是如何得到的吗? 如果忘记了,赶紧翻上去 ,看看哦! 加油!

参考文档

doc str
identifiers
count
methods

分享快乐,留住感动. 2020-03-18 20:29:39 --frank
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章