Python基础(8)字符串及常用操作

字符串(string)

1.拼接字符串

str1 = '秦始皇'
str2 = '中国'
print(str1 + '----' + str2)

结果:

秦始皇----中国

2.计算字符串长度

在Python中:
英文、数字、小数点、下划线和空格占1个字节;
汉字在GBK / GB2312编码格式中占2个字节,在UTF-8 / Unicode编码中一般占用3个字节。
但是默认情况下计算字符串长度时,不区分英文、汉字和数字,统统按1个字符计算。
如下所示:

str = '人生苦短,我用Python'
print(len(str))

结果:

13

当指定UTF-8编码时,如下:

str = '人生苦短,我用Python'
print(len(str.encode()))         # UTF-8编码

结果:

27

当指定GBK编码时,如下:

str = '人生苦短,我用Python'
print(len(str.encode('gbk')))     # GBK编码

结果:

20

3.截取字符串

语法:string[start: end: step]

str = '人生苦短,我用Python'
print(str[0: 15: 2])

结果:

人苦,用yhn

4.分割字符串

str.split(sep, maxsplit)
str: 要分割的字符串
sep: 指定分隔符,可以包含多个字符,默认为None,即所有空字符(包括空格、换行符’\n’,制表符’\t’等)
maxsplit: 可选参数,指定分割次数,如果不指定或者为-1,则分割次数没有限制,否则返回结果列表的元素个数,个数最多为maxsplit + 1
(1)如果不指定分割符,且字符串中没有默认的分割符,结果会输出原字符串

str = '人生苦短,我用Python'
a = str.split()
print(a)

结果:

['人生苦短,我用Python']

(2)如果不指定分割符,字符串中有默认的分割符,结果会进行分割

str = '人 生  苦   短    ,我     用Python'
a = str.split()
print(a)

结果:

['人', '生', '苦', '短', ',我', '用Python']

(3)如果指定分割符,结果会进行分割

str = '人生苦短,我用Python'
a = str.split(',')
print(a)

结果:

['人生苦短', '我用Python']

默认采用空白符进行分割

5.合并字符串

字符串的合并和字符串的拼接不同,字符串的合并会将字符串按照固定的分割符拼接在一起,字符串的拼接只是纯粹的使用“+”将字符串连接在一起。
语法:new_str = a.join(iterable)
new_str:合并后的新字符串
a:拼接符,属于字符串类型,书写时记得带上引号
iterable:可迭代对象

list = ['中国', '俄罗斯', '以色列', '南非']
str = 'and &'.join(list)
print(str)

结果:

中国and &俄罗斯and &以色列and &南非

6.检索字符串

(1)count()

语法:str.count(sub)
str:原字符串
sub:要检索的子字符串

str = 'i am a student, he is also a student'
a = str.count('student')
print(a)

结果:

2
(2)find()

检索是否包含指定的字符串,如果检索的字符串不存在,返回-1,如果存在,则返回首次出现该字符串的索引。
语法:str.find(sub)

str = 'i am a student, he is also a student'
a = str.find('b')
print(a)

结果:

-1

以上代码因为原字符串不包含字符串b,所以返回-1。

str = 'i am a student, he is also a student'
a = str.find('a')
print(a)

结果:

2

以上代码因为原字符串包含字符串a,所以返回首次出现该字符串的索引值。

还有一个rfind()方法,与find()方法类似,只不过rfind()方法是从字符串右边开始查找。

(3)in/not in

判断是否包含某段字符串还可以用in / not in来判断,返回结果是布尔类型的True或者False
语法:子字符串 in 字符串

str = 'i am a student, he is also a student'
print('student' in str)

结果:

True
(4)index()

index()方法与find()方法类似,都是用来查找是否包含某段字符串,只不过index()方法当某段字符串不存在时,会抛出异常。
语法:str.index(sub)
首先来看某段字符串存在时:

str = 'i am a student, he is also a student'
a = str.index('he')
print(a)

结果:

16

其次,看下某段字符串不存在时:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/python_basic/homework/homework1.py", line 2, in <module>
    a = str.index('hello')
ValueError: substring not found
(5)startswith()

检索字符串是否以指定的子字符串开头,如果是返回True,如果不是返回False。
语法:str.startswith(sub)
str:原字符串
sub:要检索的子字符串

str = 'i am a student, he is also a student'
a = str.startswith('i')
print(a)

结果:

True

注意:这个单词是startswith(),不是startwith(),中间还有一个s,下面endswith()同理

(6)endswith()

检索字符串是否以指定的子字符串结尾,如果是返回True,如果不是返回False。
语法:str.endswith(sub)
str:原字符串
sub:要检索的子字符串

str = 'i am a student, he is also a student'
a = str.endswith('udent')
print(a)

结果:

True

7.字母的大小写转换

(1)lower()

语法:str.lower()

str = 'I am a STUDENT, he is also a student'
a = str.lower()
print(a)

结果:

i am a student, he is also a student
(2)upper()

语法:str.upper()

str = 'I am a STUDENT, he is also a student'
a = str.upper()
print(a)

结果:

I AM A STUDENT, HE IS ALSO A STUDENT

8.去除字符串中的空格和特殊的字符

(1)strip()

去除字符串左右两边的空格和特殊字符。
语法:str.strip([chars])
str:原字符串
chars:可选参数,去除指定的字符,可以指定多个。如果指定参数,则去除所指定的参数;如果没有指定参数,则默认去除空格、制表符\t,回车符\r,换行符\n

首先不设置参数:

str = '   @@..$I am a STUDENT, he is also a student..()   \t'
a = str.strip()
print(a)

结果:

@@..$I am a STUDENT, he is also a student..()

其次,设置参数:

str = '   @@..$I am a STUDENT, he is also a student..()   \t'
a = str.strip(' @.$()\t')
print(a)

结果:

I am a STUDENT, he is also a student
(2)lstrip()

去除字符串左边的空格和特殊字符
语法同strip()类似

str = '   @@..$I am a STUDENT, he is also a student..()   '
a = str.lstrip(' @.$()\t')
print(a)

结果:

I am a STUDENT, he is also a student..()   *
(3)rstrip()

去除字符串右边的空格和特殊字符
语法同strip()类似

str = '   @@..$I am a STUDENT, he is also a student..()   *'
a = str.rstrip(' .()*')
print(a)

结果:

   @@..$I am a STUDENT, he is also a student

9.格式化字符串

(1)使用%操作符
(2)format()

语法:str.format(args)
str:原字符串
args:指定要转换的项,如果有多项,则用逗号进行分隔。

1)字段名字为整数,表示参数的位置
print("my name is {0} and my age is {1} years old".format('sunlin', '100'))

输出:

my name is sunlin and my age is 100 years old

2)字段名字为参数的名字
print("my name is {mingzi} and my age is {nianling} years old".format(mingzi = 'sunlin', nianling = '100'))

输出:

my name is sunlin and my age is 100 years old

3)列表的取值
print("my name is {0[0]} and my age is {0[1]} years old".format(['sunlin', 100]))
print("my name is {[1]} and my age is {[2]}".format(['sunlin', 'dagou', 'ergou'], [21, 34, 47]))

输出:

my name is sunlin and my age is 100 years old my name is dagou and my
age is 47

4)字典的取值
infor = {'ming_zi': 'sunlin', 'nian_ling': '24'}
print("my name is {ming_zi} and my age is {nian_ling}".format(**infor))

输出:

my name is sunlin and my age is 24

10.替换字符串 replace()

语法:str.replace(‘要替换的子字符串’, ‘替换的符号’)

str = '中国共产党'
a = str.replace('共产党', '*')
print(a)

结果:

中国*
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值