数据类型-字符串

字符串是 python最常见的基本数据类型之一,常见的定义方式是一对单引号( '……')或者一对双引号 ("……")创建,多行字符串也可使用三单引号或者三双引号定义。

1. 特点

不可变类型: 在元素定义之后不能对其进行修改,否则会报错

可以进行切片和索引操作: 索引下标从零开始

示例

a = "hello world"

b = 'hello python'

print(type(a), type(b))

print(type(a), type(b)) 打印输出结果显示,可以看出a变量和b变量的类型都是str字符串,type内置函数用来查看变量或者对象的类型。

2. 索引 和 切片

下标又叫索引,用来记录元素的位置,方便通过下标快速找出对应的的数据,字符串的下标是从 0 开始,在使用过程中要注意索引越界问题

语法

序列[索引位置]

示例

print(a[0])

print(a[1])

如果索引值超过字符串的长度,程序会抛出错误IndexError: string index out of range

切片是指对操作的对象截取一部分,切片索引越界不会报错,如果超过元素的最大索引,就按最大索引返回

语法

序列[开始位置下标:结束位置下标:步长]

name = "abcdefg"

print(name[2:5:1]) # cde

print(name[2:5]) # cde

print(name[:5]) # abcde

print(name[1:]) # bcdefg

print(name[:]) # abcdefg

print(name[::2]) # aceg

print(name[:-1]) # abcdef, 负1表示倒数第一个数据

print(name[-4:-1]) # def

print(name[::-1]) # gfedcba

切片满足含头不含尾的规则,正负数都是一样

步长为可选参数,如果不传,默认为1自增

3. 字符串格式

3.1 %号占位符

字符串具有一种特殊的内置操作:使用 % (取模) 运算符。 这也被称为字符串的 格式化 或 插值 运算符。

转换符

描述

d

有符号十进制整数

i

有符号十进制整数

o

有符号八进制数

f

浮点十进制格式

r

字符串(使用repr()转换任何 Python 对象)

s

字符串(使用str()转换任何 Python 对象)

a

字符串(使用ascii()转换任何 Python 对象)

%

不转换参数,在结果中输出一个 '%' 字符

age = 18

name = 'TOM'

weight = 75.5

stu_id = 1

stu_id2 = 1000

# 1. 今年我的年龄是x岁 -- 整数 %d

print('今年我的年龄是%d岁' % age)

# 结果: 今年我的年龄是18岁

# 2. 我的名字是x -- 字符串 %s

print('我的名字是%s' % name)

# 结果:我的名字是TOM

# 3. 我的体重是x公斤 -- 浮点数 %f

print('我的体重是%.3f公斤' % weight)

# 结果: 我的体重是75.500公斤

# 4. 我的学号是x -- %d

print('我的学号是%d' % stu_id)

# 结果: 我的学号是1

# 4.1 我的学号是001

print('我的学号是%03d' % stu_id)

print('我的学号是%03d' % stu_id2)

# 结果: 我的学号是001

# 结果: 我的学号是1000

# 5. 我的名字是x,今年x岁了

print('我的名字是%s,今年%d岁了' % (name, age))

# 结果: 我的名字是TOM,今年18岁了

# 5.1 我的名字是x,明年x岁了

print('我的名字是%s,明年%d岁了' % (name, age + 1))

# 结果: 我的名字是TOM,明年19岁了

# 6. 我的名字是x,今年x岁了,体重x公斤,学号是x

print('我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%06d' % (name, age, weight, stu_id))

# 结果: 我的名字是TOM,今年18岁了,体重75.50公斤,学号是000001

3.2 字符串的 format() 方法

age = 18

name = 'TOM'

weight = 75.5

stu_id = 1

stu_id2 = 1000

print('我的名字是{},今年{}岁了,体重{}公斤,学号是{}'.format(name, age, weight, stu_id))

print('我的名字是{name},今年{age}岁了,体重{weight}公斤,学号是{stu_id}'.format(stu_id=stu_id, weight=weight, name=name, age=age))

花括号和其中的字符(称为格式字段)将替换为传递给 str.format()方法的对象。花括号中的数字可用来表示传递给 str.format() 方法的对象的位置。

3.3 f {} 格式字符串

age = 18

name = 'TOM'

weight = 75.5

stu_id = 1

stu_id2 = 1000

print(f'我的名字是{name},今年{age}岁了,体重{weight}公斤,学号是{stu_id}'))

f 格式字符串的方法可以说就是str.format()方法的简写形式

4 字符串常用方法

4.1 查找

对于字符串来说,查找一个元素的位置应该经常使用的,查找就是找出元素在在字符串中出现的位置或者出现的次数

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

语法

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

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.find('and')) # 12

print(mystr.find('and', 15, 30)) # 23

print(mystr.find('ands')) # -1

开始位置下标,结束位置下标都是可以省略的,不传递参数默认就是在整个字符串中查找

index(): 检测某个子串是否包含在这个字符串中,如果存在返回这个子串开始的位置下标,不存在则报异常。

语法

字符串序列.index(子串, 开始位置下标, 结束位置下标)

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.index('and')) # 12

print(mystr.index('and', 15, 30)) # 23

print(mystr.index('ands')) # 报错 ValueError:

rfind(): 和find()功能相同,但查找方向为 右侧 开始。

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

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

语法

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

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.count('and')) # 3

print(mystr.count('ands')) # 0

print(mystr.count('and', 0, 20)) # 1

4.2 修改

字符串是不可变类型,修改指的是通过函数的形式修改字符串中的数据

replace():替换

语法

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

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.replace('and', 'he'))

# 结果:hello world he itcast he itheima he Python

print(mystr.replace('and', 'he', 10))

# 结果:hello world he itcast he itheima he Python

print(mystr)

# 结果:hello world and itcast and itheima and Python

替换次数可以不传,默认就是全部字符串中匹配上了就改变, 如果传入替换次数,查出子串出现次数,则替换次数为该子串出现次数

数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

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

语法

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

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

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.split('and'))

# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']

print(mystr.split('and', 2))

# 结果:['hello world ', ' itcast ', ' itheima and Python']

print(mystr.split(' '))

# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']

print(mystr.split(' ', 2))

# 结果:['hello', 'world', 'and itcast and itheima and Python']

如果分割字符是原有字符串中的子串,分割后则丢失该子串。

join():用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串。

语法

字符或子串.join(多字符串组成的序列)

示例

list1 = ['I', 'love', 'you']

t1 = ('aa', 'b', 'cc', 'dd')

print('_'.join(list1))

# 结果:I_love_you

print('...'.join(t1))

# 结果:aa...b...cc...ddd

capitalize():将字符串第一个字符转换成大写。

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.capitalize())

# 结果:Hello world and itcast and itheima and python

capitalize()函数转换后,只字符串第一个字符大写,其他的字符全都小写。

title():将字符串每个单词首字母转换成大写。

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.title())

# 结果:Hello World And Itcast And Itheima And Python

lower():将字符串中大写转小写。

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.lower())

# 结果:hello world and itcast and itheima and python

upper():将字符串中小写转大写。

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.upper())

# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON

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

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

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

ljust():返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串。

语法

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

示例

mystr = 'hello'

mystr1 = mystr.ljust(10, '.')

print(mystr1)

# 结果:hello.....

rjust():返回一个原字符串右对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串,语法和ljust()相同。

center():返回一个原字符串居中对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串,语法和ljust()相同。

示例

mystr = 'hello'

mystr1 = mystr.center(10, '.')

print(mystr1)

# 结果: ..hello...

4.3 判断

判断是指符合条件与否,返回的结果是布尔类型即True或者False

startswith():检查字符串是否是以指定子串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。

语法

字符串序列.startswith(子串, 开始位置下标, 结束位置下标)

示例

mystr = "hello world and itcast and itheima and Python "

print(mystr.startswith('hello'))

# 结果:True

print(mystr.startswith('hello', 5, 20))

# 结果False

endswith()::检查字符串是否是以指定子串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。

语法

字符串序列.endswith(子串, 开始位置下标, 结束位置下标)

示例

mystr = "hello world and itcast and itheima and Python"

print(mystr.endswith('Python'))

# 结果:True

print(mystr.endswith('python'))

# 结果:False

print(mystr.endswith('Python', 2, 20))

# 结果:False

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

示例

mystr1 = 'hello'

mystr2 = 'hello12345'

print(mystr1.isalpha())

# 结果:True

print(mystr2.isalpha())

# 结果:False

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

示例

mystr1 = 'aaa12345'

mystr2 = '12345'

print(mystr1.isdigit())

# 结果: False

print(mystr2.isdigit())

# 结果:False

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

示例

mystr1 = 'aaa12345'

mystr2 = '12345-'

print(mystr1.isalnum())

# 结果:True

print(mystr2.isalnum())

# 结果:False

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

mystr1 = '1 2 3 4 5'

mystr2 = ' '

print(mystr1.isspace())

# 结果:False

print(mystr2.isspace())

# 结果:True