(Python学习笔记):字符串
发布日期:2021-05-07 15:19:40 浏览次数:25 分类:精选文章

本文共 2537 字,大约阅读时间需要 8 分钟。

Python字符串操作指南

##认识字符串

在Python中,字符串是最常用的数据类型之一。要创建一个字符串,只需为变量分配一个值即可。例如:

a = 'hello world'
print(a)
print(type(a))

字符串可以用单引号或双引号表示。以下是两种常见方式:

b = "TOM"
print(type(b))

需要注意的是,字符串是不可变的类型,任何修改操作都会生成新字符串。

##字符串特征

字符串具有以下特征:

  • 一对引号包围字符串(单引号或双引号)
  • 三引号字符串(用于多行内容)
  • 注意引号转义的使用
  • 例如:

    e = '''i am TOM'''
    print(type(e))
    print(e)
    f = """I am TOM"""
    print(type(f))
    print(f)

    ##字符串输出

    使用print函数输出字符串,可以使用格式化字符串。例如:

    print("hello world")
    name = 'ROSE'
    print('我的名字是%s' % name)
    print(f'我的名字是{name}')

    ##字符串输入

    通过input()函数获取用户输入。例如:

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

    ##下标(索引)

    字符串支持通过下标访问字符。索引从0开始。例如:

    str1 = 'abcdefg'
    print(str1)
    print(str1[0])
    print(str1[1])

    需要注意的是,负数索引从末尾开始计算。

    ##切片(左闭右开)

    切片操作可以截取字符串的一部分。语法为:

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

    例如:

    str1 = '012345678'
    print(str1[2:5:1]) # 234
    print(str1[2:5:2]) # 24
    print(str1[2:5]) # 234
    print(str1[:5]) # 01234
    print(str1[2:]) # 2345678
    print(str1[:]) # 012345678
    print(str1[::-1]) # 876543210
    print(str1[-4:-1]) # 567

    需要注意的是,切片操作不会包含结束位置下标对应的字符。

    ##常用操作方法

    ###查找

    ####find() 检测子串是否存在,返回起始位置或-1。

    mystr = "hello world and itcast and itheima and Python"
    print(mystr.find('and')) # 12
    print(mystr.find('ands')) # -1

    ####index() 与find()功能相同,但若子串不存在则报异常。

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

    ####rfind()和rindex() 查找子串在右侧的位置。

    print(mystr.rfind('and'))  # 23
    print(mystr.rindex('and')) # 23

    ####count() 返回子串在字符串中的出现次数。

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

    ###修改

    ####replace() 替换子串。

    mystr = "hello world and itcast and itheima and Python"
    new_str = mystr.replace('and', 'he')
    print(new_str)

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

    print(mystr.split('and'))  # ['hello world ', ' itcast ', ' itheima ', ' Python']
    print(mystr.split(' ', 2)) # ['hello', 'world', 'and itcast and itheima and Python']

    ####join() 合并多个字符串。

    mylist = ['aa', 'bb', 'cc']
    new_str = '...'.join(mylist)
    print(new_str)

    ###判断

    ####startswith()和endswith()

    判断字符串是否以特定子串开头或结尾。

    print(mystr.startswith('hello'))  # True
    print(mystr.endswith('Python')) # True

    ####isalpha()、isdigit()、isalnum()和isspace()

    判断字符串的属性。

    mystr1 = 'hello'
    mystr2 = 'hello12345'
    print(mystr1.isalpha()) # True
    print(mystr2.isalnum()) # True

    ####lstrip()、rstrip()和strip()

    去除左右或两侧的空白字符。

    mystr = "   hello world and itcast and itheima and Python   "
    print(mystr.lstrip()) # 'hello world and itcast and itheima and Python'
    print(mystr.strip()) # 'hello world and itcast and itheima and Python'

    ##判断

    判断字符串的真假性,返回布尔值。

    if 'a' in 'abc':
    print(True)
    else:
    print(False)

    通过以上方法,可以对字符串进行各种操作,满足开发需求。

    上一篇:(Python学习笔记):列表
    下一篇:(Python学习笔记):循环 -- while与for

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年03月26日 01时07分56秒