Python学习笔记(六)

字符串操作

字符串是Python中最常用的数据类型,一般用引号来创建字符串。字符串创建很简单,只要为变量分配一个值即可。

1.字符串特征

python字符串名称要自定义 python用字符串写自己的名字_子串


python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_02


例子:

a = 'hello ' \
    'world'  #单引号书写字符串
print(a)
print(type(a))

b = "Tom"  #双引号书写字符串
print(b)
print(type(b))

c = '''I am Tom.'''  #三引号书写字符串
print(c)
print(type(c))

d = """I 
am Tom."""
print(d)
print(type(d))

e = 'I\'m Tom.'  #引号是成对出现的
print(e)
print(type(e))

python字符串名称要自定义 python用字符串写自己的名字_子串_03

2.字符串输出
print('hello world')

name = 'Tom'
print('我的名字是%s'%name)  #格式化输出
print(f'我的名字是{name}')  #格式化输出
3.字符串输入

在Python中,使用input()接受用户输入

password = input('你输入的密码是:')
print(f'您输入的密码是{password}')
print(type(password))

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_04

4.下标

“下标”又叫“索引”,即为编号,下标的作用是通过下标快速找到对应的数据。

str1 = 'abcdefg'
print(str1)
#这些数据从0开始顺序分配一个编号
# str[n]
print(str1[0])

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_Python_05

5.切片

切片是对操作的对象截取其中一部分的操作。字符串,列表,元组都支持切片操作。
语法:

python字符串名称要自定义 python用字符串写自己的名字_Python_06


“左闭右开”意味着开始下标位置包括,结束下标位置不包括。

下列代码是重点!!!

# 切片
name = '01234567'
# 序列名[开始位置下标:结束位置下标:步长]

# 想得到字符串序列中某一段
print(name[2:5:1])  #234
print(name[2:5])  #234,步长为1可省略不写
print(name[:5])  #01234,如果不写开始,默认从0开始
print(name[2:])  #234567,如果不写结束,默认写到尾
print(name[:])  #01234567,如果开始和结束都不写,默认从头到尾
print(name[0:7:2])  #0246
print(name[::-1])  #76543210,如果步长为负数,倒序输出
print(name[-4:-1])  #456,最后一个位置下标为-1(不输出),往左为-2.-3...
#****如果选取的方向(下标开始到结束的方向)和步长方向冲突,则吴凡选取数据
print(name[-4:-1:-1])  #不能选取数据,:从-4开始到-1结束,选取方向从左到右;但是步长为-1,选取方向为从右到左

print(name[-1:-4:-1])  #765,选取方向一致
print(name[7:4:-1])  #765,习惯用正值下标达到相同效果
6.常用操作方法

字符串常用操作方法有查找、修改、判断三大类。
6.1查找
-find():检测某个子串是否包含在这个字符串中。
语法:

字符串.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():检测某个子串是否包含在这个字符串中。

语法:

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

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

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'))  #报错,这是与find区别

python字符串名称要自定义 python用字符串写自己的名字_子串_07


注意:没查到的时候find()和index()还是有区别的。find()返回-1,而index()报错。

python字符串名称要自定义 python用字符串写自己的名字_字符串_08

name = 'hello world and itcast and itheima and Python'
print(name.count('and'))  #3,开始、结束下标省略后表示在整个字符串序列中查找

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

print(mystr.find('and'))  #12,返回子字符串第一个字母的下标
print(mystr.rfind('and'))  #35,从右向左开始查找,但是返回的是从左向右的坐标

6.2 修改
所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。
-replace():替换
语法:

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

注意:替换次数如果省略不写,就是替换所有。
例子:

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

#replace() :把and换成he
new_str = mystr.replace('and','he')   #说明replace有返回值,返回值是修改后的数据
print(mystr)
print(new_str)
# **** 调用replace函数后,发现原有字符串的数据并没有做到修改,修改后的数据是replace的返回值
# ---- 说明 字符串是不可变的数据类型
# 数据根据是否可以改变划分为 可变类型 和 不可变类型
new_str1 = mystr.replace('and','he',10)  #替换次数如果超过子串出现次数,则表示替换所有子串
print(new_str1)

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_09


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

语法:

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

python字符串名称要自定义 python用字符串写自己的名字_字符串_10


例子:

#split() -- 分割,返回列表,但会丢失分割字符
list = mystr.split('and')
print(list)
list1 = mystr.split('and',2)
print(list1)

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_11

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

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

例子:

#join() -- 合并列表里面字符串的字符串数据为一个大字符串
mylist = ['aa','bb','cc']
#aa...bb...cc
new_str2 ='...'.join(mylist)
print(new_str2)

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_字符串_12


其他字符串修改函数之大小写:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_13


python字符串名称要自定义 python用字符串写自己的名字_Python_14


python字符串名称要自定义 python用字符串写自己的名字_Python_15

mystr = 'hello world and itcast and itheima and Python'
print(mystr)

#1.capitalize(): 字符串首字母大写
print(mystr.capitalize())

#2.title():字符串中每个单词首字母大写
print(mystr.title())

#3.upper():把小写转大写
print(mystr.upper())

#4.lower():把大写转大写
print(mystr.lower())

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_16


其他字符串修改函数之删除空白字符:

python字符串名称要自定义 python用字符串写自己的名字_Python_17

mystr = '    hello world and itcast and itheima and Python    '
print('原来:',mystr)

#1. lstrip():删除左侧空白字符
print('删左:',mystr.lstrip())

#2. rstrip():删除右侧空白字符
print('删右:',mystr.rstrip())

#3. strip():删除两侧空白字符
print('删两侧:',mystr.strip())

python字符串名称要自定义 python用字符串写自己的名字_Python_18


其他字符串修改函数之左中右对齐:

mystr = 'hello'
print(mystr)

#1. ljust():左对齐
print('左对齐:')
print(mystr.ljust(10,'.'))
#2, rjust():右对齐
print('右对齐:')
print(mystr.rjust(10,'.'))
#3. center():居中
print('居中:')
print(mystr.center(11,'.'))

python字符串名称要自定义 python用字符串写自己的名字_Python_19


6.3 判断

判断字符串是否以…开头的函数:

python字符串名称要自定义 python用字符串写自己的名字_字符串_20


语法:

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

python字符串名称要自定义 python用字符串写自己的名字_子串_21


语法:

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

例子:

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

#1.starwith():判断字符串是否以某个子串开头
print(mystr.startswith('hello'))
print(mystr.startswith('he'))
print(mystr.startswith('hels'))

#1.endwith():判断字符串是否以某个子串结尾
print(mystr.endswith('Python'))
print(mystr.endswith('python'))

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_子串_22


判断字符串是否都是字母或者数字的函数:

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_23


python字符串名称要自定义 python用字符串写自己的名字_子串_24


python字符串名称要自定义 python用字符串写自己的名字_Python_25


python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_26


例子:

str = 'hello world and itcast and itheima and Python'

# 1. isalpha():字母
str1 = 'abd'
print(str1.isalpha())
print(str.isalpha())

# 2. isdigit():都是数字
str2 = '1342'
print(str2.isdigit())
print(str.isdigit())

# 3. isalnum():都是数字或者字母的组合
str3 = '2321hshf'
print(str3.isalnum())
print(str.isalnum())

# 4.isspace():都是空格
str4 = '  '
print(str4.isspace())
print(str.isspace())

结果截图:

python字符串名称要自定义 python用字符串写自己的名字_Python_27

7.总结(重点)

python字符串名称要自定义 python用字符串写自己的名字_python字符串名称要自定义_28