Python_基础_3

1字符串

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

1.1
# 字符串
a = 'hello world'
print(a) # hello world

a = 'hello ' \
    'world'
print(a) # hello world

b = "Tom"
c = '''i love you'''

d = """I LOVE YOU"""
print(d) # I LOVE YOU


d = """I LOVE 
YOU"""
print(d)
# I LOVE
# YOU

e = "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[:]) # 0123456

print(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 += 1
    for name in office:
        print(name, end=' ')
    print()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值