字符串

字符串是 Python 中最常⽤的数据类型。我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为

变量分配⼀个值即可

  1. a = 'hello world'
  2. b = "abcdefg"
  3. print(type(a))
  4. print(type(b))

注意:控制台显示结果为 , 即数据类型为str(字符串)。

特征

⼀对引号字符串
  1. name1 = 'Tom'
  2. name2 = "Rose"

三引号字符串
  1. name3 = ''' Tom '''
  2. name4 = """ Rose """
  3. a = ''' i am Tom,
  4. nice to meet you! '''
  5. b = """ i am Rose,
  6. nice to meet you! """

注意:三引号形式的字符串⽀持换⾏

创建⼀个字符串 I’m Tom
  1. c = "I'm Tom"
  2. d = 'I\'m Tom'

输出

  1. print('hello world')
  2. name = 'Tom'
  3. print('我的名字是%s' % name)
  4. print(f'我的名字是{name}')

输⼊

  1. name = input('请输⼊您的名字:')
  2. print(f'您输⼊的名字是{name}')
  3. print(type(name))
  4. password = input('请输⼊您的密码:')
  5. print(f'您输⼊的密码是{password}')
  6. print(type(password))
  7. 1234567

下标

“下标” ⼜叫 “索引” ,就是编号。⽐如⽕⻋座位号,座位号的作⽤:按照编号快速找到对应的座位。同

理,下标的作⽤即是通过下标快速找到对应的数据。

字符串 name = “abcdef” ,取到不同下标对应的数据

  1. name = "abcdef"
  2. print(name[1])
  3. print(name[0])
  4. print(name[2])

切⽚

切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作。

语法

  1. 序列[开始位置下标:结束位置下标:步⻓]
  1. 不包含结束位置下标对应的数据, 正负整数均可;

  2. 步⻓是选取间隔,正负整数均可,默认步⻓为1。

  1. name = "abcdefg"
  2. print(name[2:5:1]) # cde
  3. print(name[2:5]) # cde
  4. print(name[:5]) # abcde
  5. print(name[1:]) # bcdefg
  6. print(name[:]) # abcdefg
  7. print(name[::2]) # aceg
  8. print(name[:-1]) # abcdef, 负1表示倒数第⼀个数据
  9. print(name[-4:-1]) # def
  10. print(name[::-1]) # gfedcba

常⽤操作⽅法

查找、修改和判断

查找

所谓字符串查找⽅法即是查找⼦串在字符串中的位置或出现的次数。

find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返

回-1。

  1. 字符串序列.find(⼦串, 开始位置下标, 结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找

  1. mystr = "hello world and itcast and itheima and Python"
  2. print(mystr.find('and')) # 12
  3. print(mystr.find('and', 15, 30)) # 23
  4. print(mystr.find('ands')) # -1

index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则报异常。
  1. 字符串序列.index(⼦串, 开始位置下标, 结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找

  1. mystr = "hello world and itcast and itheima and Python"
  2. print(mystr.index('and')) # 12
  3. print(mystr.index('and', 15, 30)) # 23
  4. print(mystr.index('ands')) # 报错

rfifind(): 和fifind()功能相同,但查找⽅向为右侧开始。

rindex():和index()功能相同,但查找⽅向为右侧开始。

count():返回某个⼦串在字符串中出现的次数

语法

  1. 字符串序列.count(⼦串, 开始位置下标, 结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找

  1. mystr = "hello world and itcast and itheima and Python"
  2. print(mystr.count('and')) # 3
  3. print(mystr.count('ands')) # 0
  4. print(mystr.count('and', 0, 20)) # 1

修改

所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。

replace():替换

  1. 字符串序列.replace(旧⼦串, 新⼦串, 替换次数)

注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:hello world he itcast he itheima he Python
  3. print(mystr.replace('and', 'he'))
  4. # 结果:hello world he itcast he itheima he Python
  5. print(mystr.replace('and', 'he', 10))
  6. # 结果:hello world and itcast and itheima and Python
  7. print(mystr)

注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候

不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

split():按照指定字符分割字符串

  1. 字符串序列.split(分割字符, num)

注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
  3. print(mystr.split('and'))
  4. # 结果:['hello world ', ' itcast ', ' itheima and Python']
  5. print(mystr.split('and', 2))
  6. # 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
  7. print(mystr.split(' '))
  8. # 结果:['hello', 'world', 'and itcast and itheima and Python']
  9. print(mystr.split(' ', 2))

join():⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串。

  1. 字符或⼦串.join(多字符串组成的序列)
  1. list1 = ['chuan', 'zhi', 'bo', 'ke']
  2. t1 = ('aa', 'b', 'cc', 'ddd')
  3. # 结果:chuan_zhi_bo_ke
  4. print('_'.join(list1))
  5. # 结果:aa...b...cc...ddd
  6. print('...'.join(t1))

capitalize():将字符串第⼀个字符转换成⼤写。

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:Hello world and itcast and itheima and python
  3. print(mystr.capitalize())

注意:capitalize()函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写

title():将字符串每个单词⾸字⺟转换成⼤写。

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:Hello World And Itcast And Itheima And Python
  3. print(mystr.title())

lower():将字符串中⼤写转⼩写。

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:hello world and itcast and itheima and python
  3. print(mystr.lower())

upper():将字符串中⼩写转⼤写。

  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
  3. print(mystr.upper())

lstrip():删除字符串左侧空⽩字符

rstrip():删除字符串右侧空⽩字符。

strip():删除字符串两侧空⽩字符。

ljust():返回⼀个原字符串左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串。

字符串序列.ljust(⻓度, 填充字符)

rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同。

center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同

判断

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。

startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开

始和结束位置下标,则在指定范围内检查。

  1. 字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)
  1. mystr = "hello world and itcast and itheima and Python "
  2. # 结果:True
  3. print(mystr.startswith('hello'))
  4. # 结果False
  5. print(mystr.startswith('hello', 5, 20))
  6. 1234567

endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开

始和结束位置下标,则在指定范围内检查。

  1. 字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)
  1. mystr = "hello world and itcast and itheima and Python"
  2. # 结果:True
  3. print(mystr.endswith('Python'))
  4. # 结果:False
  5. print(mystr.endswith('python'))
  6. # 结果:False
  7. print(mystr.endswith('Python', 2, 20))

isalpha():如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False。

  1. mystr1 = 'hello'
  2. mystr2 = 'hello12345'
  3. # 结果:True
  4. print(mystr1.isalpha())
  5. # 结果:False
  6. print(mystr2.isalpha())
  7. 1234567

isdigit():如果字符串只包含数字则返回 True 否则返回 False。

  1. mystr1 = 'aaa12345'
  2. mystr2 = '12345'
  3. # 结果: False
  4. print(mystr1.isdigit())
  5. # 结果:False
  6. print(mystr2.isdigit())

isalnum():如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回False。

  1. mystr1 = 'aaa12345'
  2. mystr2 = '12345-'
  3. # 结果:True
  4. print(mystr1.isalnum())
  5. # 结果:False
  6. print(mystr2.isalnum())

isspace():如果字符串中只包含空⽩,则返回 True,否则返回 False。

  1. mystr1 = '1 2 3 4 5'
  2. mystr2 = ' '
  3. # 结果:False
  4. print(mystr1.isspace())
  5. # 结果:True
  6. print(mystr2.isspace())