【蟒蛇】7、数据容器之字符串-学Python黑马程序员个人笔记

  1. 参考视频
  2. 字符串容器的定义和操作
  3. 字符串容器的定义和操作案例
  4. 拓展-字符串大小比较方式

参考视频

黑马程序员python教程,8天python从入门到精通

字符串容器的定义和操作

"""
演示以数据容器的角色,学习字符串的相关操作
"""
my_str = 'itheima and itcast'
# 通过下标索引取值
value = my_str[2]
value2 = my_str[-16]
print(f'从字符串{my_str}取下标为2的元素,值是:{value},取下标为-16的元素,值是:{value2}')
"""
从字符串itheima and itcast取下标为2的元素,值是:h,取下标为-16的元素,值是:h
"""

# # 同元组一样,字符串是一个:无法修改的数据容器
# my_str[2] = 'H'
"""
TypeError: 'str' object does not support item assignment
"""

# index方法
value = my_str.index('and')
print(f'在字符串itheima and itcast中查找and,其起始下标是:{value}')
"""
在字符串itheima and itcast中查找and,其起始下标是:8
"""

# replace替换方法
new_my_str = my_str.replace('it', '程序')
print(f'将字符串{my_str}, 进行替换后得到:{new_my_str}')
"""
将字符串itheima and itcast, 进行替换后得到:程序heima and 程序cast
"""

# split分割方法
my_str = 'hello python itheima itcast'
my_str_list = my_str.split(' ')
print(f'将字符串{my_str}进行split切分后得到:{my_str_list},类型是:{type(my_str_list)}')
"""
将字符串hello python itheima itcast进行split切分后得到:['hello', 'python', 'itheima', 'itcast'],类型是:<class 'list'>
"""

# 字符串的规整操作strip(去前后空格,去前后指定字符串 )
my_str = '  itheima and itcast  '
# 不传入参数,取出收尾空格
new_my_str = my_str.strip()
print(f'将字符串{my_str}被strip后,结果是:{new_my_str}')
"""
将字符串  itheima itcast  被strip后,结果是:itheima itcast
"""

my_str = '12itheima and itcast21'
# 去除字符串1和2
new_my_str = my_str.strip('12')
print(f'字符串{my_str}被strip“12”后,结果是:{new_my_str}')
# 为什么单引号‘12’后格式化出问题
"""
字符串12itheima and itcast21被strip后,结果是:itheima and itcast
"""

# 统计字符串中某个字符串出现的次数  count
my_str = 'itheima and itcast'
count = my_str.count('it')
print(f'字符串{my_str}中it出现的次数是:{count}')
"""
字符串itheima and itcast中it出现的次数是:2
"""

# 统计字符串的长度,len()
num = len(my_str)
print(f'字符串{my_str}的长度是:{num}')
"""
字符串itheima and itcast的长度是:18
"""

# 字符串的遍历


def my_str():
    """
    使用for循环遍历字符串的演示函数
    :return: None
    """
    my_str = 'itheima and itcast'  # 代码提示问题(从外部作用域隐藏名称 'my_str')
    # for 临时变量 in 数据容器
    for i in my_str:
        print(f'列表中的元素:{i}')


my_str()

# 抱歉,我看漏了一个细节。
# 你的函数名称和字符串变量名称相同,导致在函数内部可能无法访问全局变量 my_str。
# 为了解决这个问题,你可以将函数名称和字符串变量名称分开,如下所示:


def print_str():
    """
    使用for循环遍历字符串的演示函数
    :return: None
    """
    my_str = 'itheima and itcast'
    # for 临时变量 in 数据容器
    for i in my_str:
        print(f'字符串中的元素:{i}')


# 调用函数
print_str()

字符串容器的定义和操作案例

# 练习案例:分割字符串
# 给定一个字符串:“itheima itcast boxuegu“
# ·统计字符串内有多少个"it”字符
# ·将字符串内的空格,全部替换为字符:“”
# ·并按照“"进行字符串分割,得到列表
#
# 输出结果
# 字符串itheima itcast boxuegu中有:2个it字符
# 字符串itheima itcast boxuegu,被替换空格后,结果:itheimalitcast|boxuegu
# 字符串itheima|itcast|boxuegu.按照|分隔后,得到:['itheima', 'itcast', 'boxuegu']

# 提示:·count、replace、split

my_str = 'itheima itcast boxuegu'
count = my_str.count('it')
print(f'字符串{my_str}中有:{count}个it字符')
new_my_str = my_str.replace(' ', '|')
print(f'字符串{my_str},被替换空格后,结果:{new_my_str}')
my_str_list = my_str.split(' ')
print(f'字符串{new_my_str},按照|分隔后得到:{my_str_list}')

"""
字符串itheima itcast boxuegu中有:2个it字符
字符串itheima itcast boxuegu,被替换空格后,结果:itheima|itcast|boxuegu
字符串itheima|itcast|boxuegu,按照|分隔后得到:['itheima', 'itcast', 'boxuegu']
"""

拓展-字符串大小比较方式

# ASCII码表,从头到尾,一位位进行比较,其中一位大,后面就无序比较了
# abc比较abd
print(f'abd大于abc,结果:{'abd' > 'abc'}')
print(f'ab大于a,结果:{'ab' > 'a'}')
"""
abd大于abc,结果:True
ab大于a,结果:True
"""
# a比较A
print(f'a大于A,结果:{'a' > 'A'}')
"""
a大于A,结果:True
"""
# key1 比较 key2
print(f'key2大于key1,结果:{'key2' > 'key1'}')
"""
key2大于key1,结果:True
"""

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 3415226167@qq.com
资源 相册