Python基础 - 字符串的操作

字符串

字符串是指以" " 双引号, 或者' ' 单引号括起来的任意文本, 例如"abaa", '1232’凡是在引号中的都是字符串.

变量赋值字符串

>>> str='123'   # 使用单引号定义字符串
>>> str    		# 输出变量的值, 单引号括起来的是字符类型
'123'

>>> str="123"	#使用双引号定义字符串
>>> str			# 输出变量的值, 单引号括起来的是字符类型
'123'	

清除变量值 - del

想清除变量被赋予的值:

>>> str='武汉'     	# 变量赋值字符串
>>> str				# 输出变量的值, 单引号括起来的是字符类型
'武汉'


>>> del str			# 清除变量的值
>>> str
<class 'str'>		# 输出变量的值, 无返回结果

字符串转义符 - \

想在字符串中输出单引号或双引号, 需要使用\转义符

>>> s='Let\'s go'
>>> str
"Let's go"
>>> 

查看数据类型 - type

使用type可查看某个变量的数据类型

>>> str='Let\'s go'
>>> str
"Let's go"

>>> type(str)
<class 'str'>   	#str 字符串类型

>>> a=1
>>> type(a)
<class 'int'>   	# int 整数类型

设置字符串格式:精简版

%s为转换说明符, 指出了要讲值插入什么地方

>>> v1='武汉 %s, 武汉 %s !!'
>>> v2=('必胜', '加油')
>>> v1 % v2
'武汉 必胜, 武汉 加油 !!'

命名替换字符

>>> "Dear {name} , Let\'s play {game}".format(name='Dongdong' , game='LEGO')
"Dear Dongdong , Let's play LEGO"

替换字符没有名称, 可将索引作为名称

>>> '{} , {}  and {}'.format('Dongdong' , 'tom' , 'Jiang')
'Dongdong , tom  and Jiang'

>>> '{0} , {2} and {1}'.format('Dongdong' , 'tom' , 'Jiang')
'Dongdong , Jiang and tom'

对于字符的操作

字符串切片

  • 字符串也是一个可迭代对象, 也就是说每一个字符串实际上都有一个对应的索引值
  • 我们将赋值于变量的字符串, 每一个字符其实都有自己的位置编号, 从0 开始的.
    string[开始位置:终止位置:步长和方向]
>>> str='python'
>>> str[0]
'p'
>>> str[1]
'y'
>>> str[2]
't'
>>> str[3]
'h'
>>> str[4]
'o'
>>> str[5]
'n'
>>> str[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
  • 也可以倒序, 从 -1 开始
>>> str
'python'
>>> str[-1]
'n'

注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。

这里以字符串为例进行讲解,实际上列表也会具有相同的方法.

>>> str
'python'

>>> str[2:4]	#从第二位开始,取第四位之前(不含)的所有字符.
'th'
  • 步长 : 在某一段内容中, 按照指定步长跳取内容
>>> str[::2]   # 起始和结束未知未指定,默认从头到尾, 取偶数位

还可以倒着取

>>> str[::-2]
'nhy'

查找字符串内容 - find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
str.find(str, start=0, end=len(mystr))

>>> str='hello, python'		# 重新定义变量str

>>> str.find('o')   		# 在str中从头查找, 字母o的位置(第一位是0)
4
>>> str.find('o',2) 		# 在str中的第二位开始查找, 字母o的位置(第一位是0)
4
>>> str.find('o',5)			# 在str中的第五位开始查找, 字母o的位置, 那么跳过了第一个o, 找到了第二个o所在的位置
11

查找单词, 那么输出的是找到单词的第一个字符的位置 , 我们知道python单词一共是6给字符

>>> str.find('python')
7
>>> str[7:7+6+1] 	# 为什么是7+6+1 , 7-是从第七位之后向后找6位, 由于是左开右闭, 那么还要再多加1位
'python'

统计字符串中, 字符出现的次数 - count

返回 str在start和end之间 在 mystr里面出现的次数

str.count(str, start=0, end=len(mystr))

>>> str 			# 变量定义字符串为 hello, python
'hello, python'

>>> str.count('o') 	# 变量中字母'o', 出现了几次.
2
>>> str.count('o',0,5) # 变量中第0个字符到第5个字符, 之间'o'出现了几次
1
>>> str.count('pyth')  # 变量中'pyth'字符串, 出现过几次
1

替换 - replace

把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

mystr.replace(str1, str2, mystr.count(str1))

替换, 不会改变原变量的值

>>> str
'hello, python'

>>> str.replace('l','加油')    # 找到str中所有的字母l, 替换为'加油'
'he加油加油o, python'
 
>>> str.replace('o','武汉',1)   # 找到str中第一个的字母o, 替换为'加油'
'hell武汉, python'

去除字符收尾空格 - strip

>>> str='Hello , python    '
>>> str
'Hello , python    '
 
>>> str.strip()
'Hello , python' 

分隔符切片 - split

将一串连续的字符串, 按照指定的字符, 分隔开
str.split(str=" ", 2)

>>> str
'hello, python'
>>> 
>>> str.split('')		# 字母o 作为分隔符
['hell', ', pyth', 'n']
>>> 
>>> str				# 变量原值不受影响
'hello, python'

分隔符切片 - partition

将一串连续的字符串, 按照一串字符, 分隔开
str.split(str=" ", 2)

>>> str
'hello, python'

>>> str.partition('lo')
('hel', 'lo', ', python')

居中对齐 - center

>>> str.center(30)		# 一共30个字符,其中字符串居中对齐显示
'        hello, python         '
>>> 
>>> str.center(30,'-')		# 一共30个字符,其中字符串居中对齐显示, 两边填充指定字符
'--------hello, python---------'
>>> 

转换大小写 - lower & upper & title

>>> str.upper()		# 转换为大写
'HELLO, PYTHON'

>>> str.title()		# 首字母大写
'Hello, Python'
发布了23 篇原创文章 · 获赞 0 · 访问量 4481

猜你喜欢

转载自blog.csdn.net/strawberry1019/article/details/104532218