1字符串

1.认识字符串
2.下标
3.切片
4.常用操作

1.1
# 字符串
a = 'hello world'
print(a) # hello worlda = 'hello ' \'world'
print(a) # hello worldb = "Tom"
c = '''i love you'''d = """I LOVE YOU"""
print(d) # I LOVE YOUd = """I LOVE
YOU"""
print(d)
# I LOVE
# YOUe = "I'm"
f = 'I'm' # 报错
d = 'I\'m' # 正确
输入与输出
name = 'Tom'print('我的名字四%s' % name)
print(f'物品的名字是{name}')name1 = input('请输入您的名字')
print(f'您的名字是{name1}')
1.2下标
str1 = 'abcdefg'
print(str1[0]) #a
1.3切片
str1 = 'abcdefg'
print(str1[0]) #a# 序列[开始位置下标:结束位置下标:步长]
'''
不包含结束位置下标对应的数据,正负整数都可以
步长是选取的间隔,正负整数都可以,默认步长为1,可以不写
'''name = '0123456'print(name[2:5:1]) # 234
print(name[2:5:2]) # 24
print(name[2:5]) # 234
print(name[:5]) # 01234  不写开始默认从0 开始
print(name[2:]) # 23456
print(name[:]) # 0123456print(name[::-1]) # 6543210  步长为负数,表示倒序取数
print(name[-4:-1]) # 345  下标-1表示最后一个数据print(name[-4:-1:-1]) # 不能选取出数据,从-4到-1,选取方向为从左往右,步长-1表示选取方向从右往左
#选取方向与步长方向相反,则不能选取出来
print(name[-1:-4:-1])

字符串类型的数据修改的时候不能改变原有字符串,属于不可变类型
特别重要

1.4.1查找
# 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()print(mystr.index('and'))  # 12
print(mystr.index('and', 15, 30))  # 23
# print(mystr.index('ands'))  # 报错# 3.count()
print(mystr.count('and'))  # 1
print(mystr.count('and', 15, 30))  # 3
print(mystr.count('ands'))  # 0# 4 rfind
print(mystr.rfind('and'))  # 35# 5 rindex
print(mystr.rindex('and'))  # 35
1.4.2修改
#  1 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', 2))  # hello world he itcast he itheima and Python
print(mystr)  # hello world and itcast and itheima and Python
'''
数据按照是否直接修改分为可变类型和不可变类型。
字符串类型的数据修改的时候不能改变原有字符串,属于不可变类型
'''# 2 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']# 3 join(多字符串组成的序列)  用一个字符或子串合并字符串,将多个字符串合并为一个新的字符串
mylist = ['aa', 'bb', 'cc']
new_str = '...'.join(mylist)
print(new_str) # aa...bb...cc#  1 capitalize() 首字符大写
mystr = 'hello world and itcast and itheima and Python'print(mystr.capitalize())  # Hello world and itcast and itheima and python# 2 title() 将每个单词的首字母大写
print(mystr.title())  # Hello World And Itcast And Itheima And Python
print(mystr)  # hello world and itcast and itheima and Python# 3 lower()  大写->小写
# 4 upper() 小写->大写
# 5 lstrip()删除字符串左侧的空白字符  rstrip()删除字符串右侧的空白字符  strip()删除字符串两侧的空白字符
str1 = '  I   '
print(str1.lstrip())  # I   //
print(str1.rstrip())  #   I
print(str1.strip())  #  I# 6 ljust(长度,填充字符) 返回一个原字符串左对齐,并使用指定字符(默认空格)填充值对应长度的新字符串
# 同理 rjust,右对齐  center 居中
str2 = 'hello'
print(str2.ljust(10, '.'))  # hello.....
1.4.3判断
# 1 startswith(子串,开始,结束)检查字符串 是否已制定子串开始
mystr = 'hello world and itcast and itheima and Python'
print(mystr.startswith('hello'))  # True
print(mystr.startswith('a'))  # False
print(mystr.startswith('world', 6))  # True# 2 isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回True
# 3 isdigit() 只包含数字  True
# 4 isalnum() 如果字符串至少有一个字符并且所有字符都是数字或字母则返回True
# 5 isspace() 只含有空白True
mystr1 = 'hello'
print(mystr.isalpha())  # False
print(mystr1.isalpha())  # True

2列表

列表可以一次性存储多个数据,且可以为不同数据类型
操作:CRUD(增删改查)

# in 判断指定数据是否在某个序列  在True   not in  不在True
print('Lily' in name_list)  # True
print('lily' in name_list)  # False
name_list = ['Tom', 'Lily', 'Rose']
# 增加数据
# 1 append() 在表尾追加数据
name_list.append('zyy')  # ['Tom', 'Lily', 'Rose', 'zyy']
print(name_list)
# 如果append追加的数据是一个序列,则追加整个序列到该列表
name_list.append(['xiaohuang'])  # ['Tom', 'Lily', 'Rose', 'zyy', ['xiaohuang']
print(name_list)# 2 extend 列表表尾追加数据,如果数据是一个序列,则将这个序列的数据逐一添加到列表
name_list.extend(['zy', 'yy'])
print(name_list)  # ['Tom', 'Lily', 'Rose', 'zyy', ['xiaohuang'], 'zy', 'yy']# 3 insert 在指定位置新增数据
name_list.insert(1, 'xia')
print(name_list)  # ['Tom', 'xia', 'Lily', 'Rose', 'zyy', ['xiaohuang'], 'zy', 'yy']
# 删除
# 1 del
name_list = ['q', 'a', 'b']
del name_list
# print(name_list)  # name 'name_list' is not defined
name_list = ['q', 'a', 'b']
del name_list[0]
print(name_list)  # ['a', 'b']# 2 pop  删除指定下标的数据,如果不指定下标,默认删除最后一个数据,返回值是被删除的数据
name_list.extend(['c', 'd'])
print(name_list.pop())  # d
print(name_list)  # ['a', 'b', 'c']
name_list.pop(0)
print(name_list)  # ['b', 'c']# 3 remove
name_list.remove('b')
print(name_list)  # ['c']# 4 clear
name_list.clear()
print(name_list)  # []
# 修改
name_list = ['a', 'b', 'c', 'd']# 1 修改指定下标的数据
name_list[0] = 'e'
print(name_list)  # ['e', 'b', 'c', 'd']# 2 逆置 reverse
num_list = [1, 2, 3, 5, 6]
num_list.reverse()
print(num_list)   # [6, 5, 3, 2, 1]# 3 排序 sort(key = None, reverse = False  reverse = True降序 reverse = False升序(默认)
num_list.sort()
print(num_list)  # [1, 2, 3, 5, 6]
num_list.sort(reverse=True)
print(num_list)  # [6, 5, 3, 2, 1]# 复制 copy
name_list = name_list = ['a', 'b', 'c', 'd']
name_list1 = name_list.copy()
print(name_list1)  # ['a', 'b', 'c', 'd']
# 列表的遍历
# 1 while
num_list = [1, 2, 3, 4, 5, 3]i = 0
while i < len(num_list):print(num_list[i], end=' ')i += 1
else:print()# 2 for
for i in num_list:print(i, end=' ')
# 列表的嵌套
name_list = [['小明', '小红', '小绿'], ['tom', 'll', 'rr'], ['张三', '里斯', '王五']]
# 找里斯
print(name_list[2][1])
# 综合运用 8位老师,三个办公室,随机分配
import random
teachers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
offices = [[], [], []]for name in teachers:num = random.randint(0, 2)offices[num].append(name)
i = 1
for office in offices:print(f'办公室{i}的人数是{len(office)},老师分别是:')i += 1for name in office:print(name, end=' ')print()

Python_基础_3相关推荐

  1. Python_基础知识储备

    目录 目录 前言 初识Python 解析型与编译型 OOP与POP 相关概念1 Python的解释器 Python程序设计的思想 Python的编程风格 最后 前言 前面的博文从记录了如何Setup ...

  2. python语法基础知识-python_基础知识_安装和基础语法

    一.python安装 先在官网,找到所属系统的安装环境.建议选择3.x 建议选择安装稳定版本. 选择自行配置安装环境. 直接选择Next,进行下一步. 选择安装目录. 安装页面,等待安装. 安装完成, ...

  3. Python_基础_6

    1函数基础 def sel_func():print('显示余额')print('存款')print('取款')print('恭喜您登录成功') sel_func() print('您的余额是9.99 ...

  4. Python_基础知识02

    1. 主要内容 回顾复习 程序结构--循环结构[while] break关键字 continue关键字 else关键字 案例操作 2. 课堂内容 1) 回顾复习 编程基础结构:程序结构 l  顺序结构 ...

  5. Python_基础语法_字符串基本操作__声明方式_占位符_切片_转义字符_加密解密(6)

    目录: 一.字符串介绍 1.字符的渲染 2.字符的编码 3.不可变类型 二.字符串的基本操作 1.字符串声明 2.字符串占位符号 3.字符串索引切片(包含练习) 4.字符串转义 三.字符串的高级操作 ...

  6. Python_基础_5

    1公共操作 str1 = 'aa' str2 = 'bb'list1 = [1, 2] list2 = [10, 20]t1 = (1, 2) t2 = (10, 20)dict1 = {'name' ...

  7. Python_基础_4

    1元组 # 一个元组可以存储多个数据,但是元组内的数据不可以修改 # 多个数据元组 t1 = (10, 20, 30) # 单个数据元组 t2 = (10,)# 查找 tuple1 = ('aa', ...

  8. Python_基础_2

    1条件语句 ''' 语法 if条件:条件成立执行的代码1... 有缩进得到属于if语句条件块 '''age = int(input('请输入您的年龄:')) #例子 if age >= 18:p ...

  9. Python_基础_1

    1.注释 注释分类:单行注释和多行注释 单行注释: 快捷键 Ctrl+/ #知识内容 多行注释: 有两种写法:六个单引号或者六个双引号 ''' 在这里插入代码片1 ''' ""&q ...

最新文章

  1. 火狐推荐几个实用的插件
  2. bzoj 3223: Tyvj 1729 文艺平衡树
  3. openresty入门示例
  4. python生成数据
  5. Codeforce 1255 Round #601 (Div. 2) A. Changing Volume (贪心)
  6. Android模拟器之神奇Genymotion的安装
  7. 用联发科芯片的手机能升级鸿蒙吗,华为鸿蒙系统降临!首批升级手机确定,联发科芯片被放弃?...
  8. 可用等式为:html+java=jsp表示jsp[8]._在 JSP 中 , 对 jsp:setProperty 标记描述正确的是 ()_学小易找答案...
  9. 用SQL实现取员工日工作量和月工作量
  10. U盘无法格式化(提示U盘文件系统变为了RAW格式)【一般应用】
  11. excel2003打开后找不到工作表
  12. 台式计算机的选购注意事项,笔记本电脑选购注意事项大全
  13. 520表白纪念自适应单页源码
  14. java中bean的作用域有哪些_Spring中Bean的作用域
  15. 汇川H5U PLC通过 EtherCAT总线控制伺服回原
  16. 高仿红孩子网上商城服务端和客户端应用源码
  17. Android-性能优化-UI优化
  18. js网页时钟(附素材抠图)
  19. ZZULIOJ 2505: 建国的嘱咐(本场签到题)
  20. java毕业设计 springboo影视播放在线视频点播系统 springboot毕业设计题目课题选题 springboot毕业设计项目作品源码(4)后台管理系统功能和界面

热门文章

  1. 20个非常有用的Java程序片段--转
  2. /bin/bash^M: bad interpreter: 没有那个文件或目录--转载
  3. npm install 时--save-dev和--save的区别
  4. NGINX Config
  5. 房地产还有最后十年机会 抓紧时间转型
  6. 为什么说在KMP算法中文本串中的每个字符都是需要进行比较操作的?
  7. Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析
  8. MySQL-获取有性能问题SQL的方法_慢查询 实时获取
  9. Algorithms_入门基础_时间复杂度空间复杂度
  10. Spring Cloud【Finchley】-18 Zuul过滤器