
(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]) # 234print(str1[2:5:2]) # 24print(str1[2:5]) # 234print(str1[:5]) # 01234print(str1[2:]) # 2345678print(str1[:]) # 012345678print(str1[::-1]) # 876543210print(str1[-4:-1]) # 567
需要注意的是,切片操作不会包含结束位置下标对应的字符。
##常用操作方法
###查找
####find() 检测子串是否存在,返回起始位置或-1。
mystr = "hello world and itcast and itheima and Python"print(mystr.find('and')) # 12print(mystr.find('ands')) # -1
####index() 与find()功能相同,但若子串不存在则报异常。
print(mystr.index('and')) # 12print(mystr.index('and', 15, 30)) # 23
####rfind()和rindex() 查找子串在右侧的位置。
print(mystr.rfind('and')) # 23print(mystr.rindex('and')) # 23
####count() 返回子串在字符串中的出现次数。
print(mystr.count('and')) # 3print(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')) # Trueprint(mystr.endswith('Python')) # True
####isalpha()、isdigit()、isalnum()和isspace()
判断字符串的属性。
mystr1 = 'hello'mystr2 = 'hello12345'print(mystr1.isalpha()) # Trueprint(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)
通过以上方法,可以对字符串进行各种操作,满足开发需求。
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月26日 01时07分56秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Spring源码:prepareBeanFactory(beanFactory);方法
2019-03-04
Spring源码:initApplicationEventMulticaster源码解析
2019-03-04
AcWing 786: 第k个数
2019-03-04
AcWing 828. 模拟栈
2019-03-04
添加Selinux权限
2019-03-04
ifconfig网络配置信息解析
2019-03-04
(2019.9.10测试可用)如何在Windows的cmd中使用ls命令
2019-03-04
债券中的久期是什么意思
2019-03-04
(20200328已解决)从docker容器内复制文件到宿主机
2019-03-04
理解Docker ulimit参数
2019-03-04
理解Python系统下的时间格式
2019-03-04
Python语言'类'概念再理解
2019-03-04
OpenAI Gym简介及初级实例
2019-03-04
Ubuntu 18.04 zip压缩文件及其文件 夹中的所以 内容
2019-03-04
int 转 CString
2019-03-04
Edit编辑框自动换行与长度
2019-03-04
低通滤波器的设计
2019-03-04
窄带随机过程的产生
2019-03-04
随机四则运算
2019-03-04