Python3入门基础:第七篇(字符串)

一.字符串的定义:

1.使用单引号或双引号括起来的(注:单引号和双引号必须成对出现,不能是一边单引号一边双引号)

   a = 'hello world'
   b =  "hello world"
   c = "hello world'
   a  b 都为字符串,而c的写法语句错误

2.当字符长的时候或者你想写成两行,可以用六个单引号或双引号

  d = '''I want to learn python
 and find a good job'''
  e = """I want to learn python
 and find a  good job"""
  f = 'I want to learn python
 and find a good job'
d e 都为字符串 f语法错误(因为在python 当中,它会被视为两行代码)
print(d)
打印结果:
I want to learn python 
and find a good job

3.如果写代码时想将一行字符串放到多行显示,但在真真生成字符串的时候,不需要换行,可以在每一行末尾加上 \

d = '''I want to learn python \
and find a good job'''
print(d)

打印结果:
I want to learn python and find a good job

4.在python3中,字符串分为2中类型,为别为str和bytes,前者是unicode字符串,后者是经过编码后的字符串,是一种字节码,适用于保存在磁盘上或者网络上传输

text = b'hello world'
print(text)
print(type(text))

打印结果:
b'hello world'
<class 'bytes'>

二.字符串的拼接

1.使用加号形式

a = 'hello'
b = ' '
c = 'world'
d = a + b + c
print(d)
打印结果:
hello world

2.使用格式化的拼接

name = "小明"
age = "23"
information = "my name is %s,my age is %s"%(name,age)
print(information)
打印结果:
my name is 小明,my age is 23

三.字符串格式化

1.使用%的形式

name = "小明"
age = "23"
information = "my name is %s,my age is %s"%(name,age)

针对不同的数据类型,应该使用不同的格式化:
1.字符串:使用%s
2.整型:使用%d
3.浮点型:使用%f,如果想要指定小数点的位数,可以使用%.nf来表示,n为1表示一位小数,n为2表示2位小数,依次类推

a = 0.23
b = "the number can show with %.3f"%a
print(b)
打印结果:
the number can show with 0.230

2.使用format形式

1.使用位置占位符(后面的内容要严格按照前边的顺序)

dream = "I like {},and I want to have a good {}".format('learnning','job')
print(dream)
打印结果:
I like learnning,and I want to have a good job

2.使用关键字参数占位符(不必按照前面的顺序)

dream = "I like {arg2},and I want to have a good {arg1}".format(arg1 = 'job',arg2 = 'learnning')
print(dream)
打印结果:
I like learnning,and I want to have a good job

四.字符串的相关操作

1.字符串的下标操作

a = "I want to study python"
b = a[0]
c = a[1]
d = a[2]
e = a[21]  #e与f等价 即可以正数也可以倒数,取决于哪个方便采用那种放式
f = a[-1]
print(b)
print(c)#因为下标为2时,是一个空格 所以打印结果是空
print(d)
print(e)
print(f)
    
    打印结果:
I
 
w
n
n

2.字符串的切片操作

切片:顾名思义就是对字符串进行“切”段操作,比如一段很长的字符串,你只需要其中的一段或几段,就可以对字符串进行切片处理,将它摘出来。
字符串的切边操作和它的下标操作功能紧密,切片会用到字符串的下标值
切片操作的语法格式:
a = [arg1:arg2:arg3]
其中 arg1 是想要的字符串片段的起始下标值
arg2 是想要的字符串片段的结束下标值
arg3 是每单个字符之间想要跨几步提取字符
例:

a = "I want to study python"
b = a[2:6]  #取下标为2到5之间的字符段  但是切片是左包右不包,即包括下标为2 的字符不包括下标为6的字符
c = a[ :9]#想要从头开始取,可以把arg1参数省略
d = a[2:]#如果不知道字符串的长度,只想从某个值取到末尾可以将arg2参数省略
e = a[-7:-1]  #切片也可以采用负数形式,道理与下标操作一样
f = a[2::2]#从第二个下标值开始每隔1个取一个字符一直取到字符串末尾,arg3为正就是从左到右跨步取值,若为负数则从右到左取值
g = a[-1::-1]
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
打印结果;
want
I want to
want to study python
 pytho
wn osuypto
nohtyp yduts ot tnaw I

3.字符串的常见操作

text = "hello python"
'''1.find:返回查找字符串的下标位置。如果返回-1则代表没有查到该字符串。 rfind是从右到左'''
position1 = text.find("o")
position2 = text.rfind("o")
position3 = text.find("haha")
print(position1)
print(position2)
print(position3)
打印结果:
    4
  10
   -1

'''2.index:用法和find类似,只不过当没有所要查的字符串,会报错(抛出异常)'''
'''3.len:获取字符串的长度'''
print(len(text))
打印结果:
12
'''4.count:获取子字符串出现的次数'''
print(text.count("o"))
打印结果:
2
'''5.replace:新创建一个字符串,把原来字符串中的某个字符替换成你想要的字符串'''
'''6.split:按照给定的字符串进行分割。返回的是一个列表'''
'''7.startswith:判断一个字符串是否以某个字符串开始'''
'''8.endswith:判断一个字符串是否以某个字符串结束'''
'''9.lower:将字符串全部改成小写'''
'''10.upper:将字符串全部改成大写'''
'''11.strip:将字符串左右空格全部去掉'''
'''12.lstrip:将字符串左边空格去掉'''
'''13.rstrip:删除字符串右边的空格'''
'''14.partition:从str出现的第一个位置起,把字符串string分成一个三元素的元组(string_pre_str,str,string_post_str),如果string中不包含str,则string_pre_str == string'''
text = 'hello world python'
result1 = text.partition('world')
result2 = text.partition('hahaha')
print(result1)
print(result2)

打印结果:
('hello ', 'world', ' python')
('hello world python', '', '')


 '''15:isalnum:如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False'''
   '''16:isalpha:如果string至少有一个字符并且所有字符都是字母则返回True,否则返回False'''
   '''17:isdigit:如果string至少有一个字符并且所有字符都是数字则返回True,否则返回False'''
   '''18:isspace:如果string只包含空格,则返回True,否则返回False'''
   '''19:format:格式化字符串'''

五.转义字符:

在这里插入图片描述

text1 = 'hello \
world'
text2 = 'hello \nworld'
text3 = 'my friend\'s watch'
text4 = "my friend\"s watch"
text5 = "my friend\"s \twatch"#等价于按了一下tab键
text6 = "\\"
print(text1)
print(text2)
print(text3)
print(text4)
print(text5)
print(text6)
打印结果:
hello world
hello 
world
my friend's watch
my friend"s watch
my friend"s  	watch
\

六.原生字符串

原生字符串会与后边的正则表达式关系密切
定义:不会对字符串中任何字符进行转义
格式:r’xxx’

text1 = 'abc\ndef'
text2 = r'abc\ndef'
print(text1)
print(text2)
打印结果:
abc
def
abc\ndef

七.字符串编码和解码:

因为在python3中,默认的字符串都是unicode型,但unicode字符串只能在内存中存在,不能在磁盘和网络间传输数据,如果要在文件或者网络间传输数据,必须要将unicode转换为bytes类型的字符串,转换函数如下:
encode(‘utf-8’):将unicode编码成bytes类型,且编码方式采用的是utf-8
decode(‘utf-8’):将bytes解码成unicode类型,且编码方式采用的是utf-8
utf-8是一种编码方式,类似与gbk ascii码等

text = 'hello world'
#unicode编码成bytes
text_bytes = text.encode('utf-8')
print(text_bytes)
print(type(text_bytes))
打印结果:
b'hello world'
<class 'bytes'>

text_bytes = b'hello world'
#bytes解码成unicode
text = text_bytes.decode('utf-8')
print(text)
print(type(text))
打印结果:
hello world
<class 'str'>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值