day10-string总结

本文详细介绍了Python中的字典、元组、集合和字符串数据结构,包括它们的操作方法如in、notin、len、切片等。同时,深入探讨了字符编码,如ASCII和Unicode,以及ord和chr函数的使用。此外,还涵盖了字符串的加法、乘法、比较运算以及各种内置方法,如split、replace、strip等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

day10-string总结

1.review

1.1 字典

# 1)in 和 not in
# 2) len、dict、list(字典)
# 3)字典.xxx()

1.2 元组

# 容器:(数据1,数据2,数据3,....)
# 不可变的;有序的
# 元素没有要求
t1 = (100,)
t2 = 10,29, 40 ,3

# 查 -   和列表一样
x , y , z = (10 ,20 ,30)

1.3集合

# 容器:{数据1 , 数据2, 数据3 ,....}
# 可变的;无序的
# 不可变的数据:唯一(自带去重功能)
# 数字集合运算:交集(&)、并集(/)、差集(-)、对称差集(^)、子集(>=、<=)、真子集(>、<)

1.4字符串

# 容器(保存多个文字符号);''、""、''''''、""""""
# 不可变;有序的
# 元素:每个独立的符号

2. charCode

# 计算机存储数据只能存数字(存的是数字的二进制补码)

2.1 字符编码

"""
为了你能够让计算机存储文字符号,给每个符号对应了一个固定的数字,每次在需要存储这个符号的时候,就去存储这个固定的数字。
每个对应的那个数字就是这个符号的编码值。
"""

2.2 编码表 - 保存字符和字符对应编码值的表

"""
1)ASCII码表
美国信息码(只包含美国人常用的符号,总共128个)
数字字符 1  -   49
大写字符(A  -   65)
小写字母(a  -   97)
英文输入法下的特殊符号

2)Unicode编码表(python)    -   包含了世界所有国家所有名族的所有的语言符号
Unicode编码表包含ASCII(前128个字符就是ASCII码表中的内容)
中文编码范围:0x4e00 ~ 0x9fa5

"""

2.3 编码值的应用

2.3.1 chr(编码值) - 获取编码值对应的字符
print(chr(97))  # a
print(chr(0x4e00))  # 一
print(chr(0x9fa5))  # 龥

# for x in range(0x4e00,0x9fa5+1):
#     print(chr(x),end=' ')
2.3.2 ord(字符) - 获取指定字符对应的编码值
# 注意:字符指的是长度为1的字符串
print(ord('1')) # 49
print(ord('刘')) # 21016
print(ord('炽')) # 28861

char = 'a'
print(chr(ord(char) - 32))
2.3.3 编码字符:\u4位的16进制编码值
# 在字符串中提供字符的方式有两种:a.直接提供字符  b.使用编码字符
str1 = 'ab你好'

print(str1) # ab你好


# 如果知道字符编码值是多少,但我不知道字符是什么的时候,就可以使用编码字符来表示这个字符
str2 = '\u0061\u9fa5'
print(str2) # a龥

3. getChar

# 字符串获取字符的方法和列表获取元素的方法一样

3.1 获取单个字符

# 字符串[下标]
# 注意:转义的长度是1
str1 = '\thello\nword\u9fa5'
print(str1[5])
print(str1[-1])

3.2 字符串切片

# 字符串[开始下标:结束下标:步长]
str1 = 'good good study!'
print(str1[1:-2:2]) # odgo td
print(str1[-3:])

3.3 遍历字符串

for x in str1:
    print(x)

for index in range(len(str1)):
    print(index,str1[index])

4. stringOperations

4.1 字符串加法运算

str1 ='hello'
str2 ='你好'
print(str1 + str2)  # hello你好
print(str1 + ' '+ str2) # hello 你好
# 练习:将字符串中所有的数字字符都改成 +
str1 = '世界上89Kl22.9;;//sh66-==1'
# '世界上++Kl++.+;;//sh++-==+'
new_str = ''
for x in str1:
    if '0' <= x <= '9':
        new_str += "+"
    else:
        new_str += x
print(new_str)

4.2 字符串乘法运算

# 字符串 * N N * 字符串   -   让字符串元素重复N次产生一个新的字符串
str1 = 'a' * 10
print(str1)

str1 = '你好' * 5
print(str1)

4.3 字符串比较运算

# 1)比较是否相等: ==、!=
print('abc' == 'bac')   # False
# 2)比较大小
# 两个字符串比较大小,比较的是第一队不相等的元素的大小
"""
判断字符的性质
是否是数字字符:'0' <= x <= '9'
是否是小写字母:'a' <= x <= 'z'
是否是大写字母:'A' <= x <= 'Z'
是否是字母:'a' <= x <= 'z' or 'A' <= x <= 'Z'
是否是汉字:'\u4e00' <= x <= '\u9fa5'
"""
print('abc' > 'bcc')    # 'a' > 'b' False
print('a1mn' > 'abc')   # '1' > 'b' False

4.4 in 和 not in

# 字符串1 in 字符串2  -   字符串1是否是字符串2的子串(字符串2是否包含字符串1)
print('a' in 'abc') # True
print('ab' in 'abc')    # True
print('ac' in 'abc')    # False

print(10 in [10 ,20 ,30])   # True
print([10 ,20 ] in [10 ,20 ,30])    # False

4.5 相关函数

print(max('helloworld'))    # "w"
print(sorted('helloworld')) # ['d', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
4.5.2 len(字符串)
msg = '\thello\nword\u4e00'
print(len(msg)) # 12
4.5.3 str
# str(数据)   -   任何类型数据都可以转换成字符串;转换的时候是在数据的打印值外面加引号
str(100)            # '100'
str(1.23)           # '1.23'
str(True)           # 'True'

list1 = [10,20,30,40]
print(list1)        # [10, 20, 30, 40]
str(list1)          # '[10, 20, 30, 40]'

4.5.4 eval(字符串)
a = eval('100') # a = 100
b = eval('1.23')    # b = 1.23

abc = 100
c = eval('abc') # c = abc

d = eval('"hello"') # d = "hello"

a1 = eval('[10, 20, 30, 40]')           # a1 = [10, 20, 30, 40]
print(a1)
a1.append(100)
print(a1)       # [10, 20, 30, 40, 100]


a = int(input('请输入数字a的值:'))
b = int(input('请输入数字b的值:'))
value = input('请选择运算符(+、-、*、/):')
result = eval('a' + value + 'b')    # 'a+b'、'a-b'
print(result)

5.stringMethods

5.1 字符串.join(序列)

# 1.字符串.join(序列)    -   将序列中的元素用指定的字符串连接成一个新的字符串(序列中的元素必须都是字符串)
list1 = ['小明','张三','李四','王五']

resulit = '+'.join(list1)
print(resulit)  # 小明+张三+李四+王五

resulit = 'and'.join(list1)
print(resulit)  # 小明and张三and李四and王五

resulit = '**'.join('abc')
print(resulit)  # a**b**c

5.2 字符串.count(字符串2)

# 2.字符串.count(字符串2) -   统计字符串1中字符串2的个数
msg = 'how are you ? i am fine! thank you , and you'

resulit = msg.count('a')    # 4
print(resulit)

print(msg.count('you')) # 3

5.3 split

# 1)字符串.split(字符串2) -   将字符串1中所有的字符串2作为切割点对字符串1进行切割
# 2)字符串1.split(字符串2,N)      -将字符串1中前N个字符串2作为切割点对字符串1进行切割
msg = 'how are you ? i am fine! thank you , and you ?'
resulit = msg.split('you')
print(resulit)

5.4replace

# 字符串.replace(字符串2,字符串3)    -   将字符串1中的所有字符串2都替换成字符串3
# 字符串.replace(字符串2,字符串3,N)    -   将字符串1中前N个字符串2都替换成字符串3
msg = 'how are you ? i am fine! thank you , and you ?'
resulit = msg.replace('you','me')
print(resulit)  # how are me ? i am fine! thank me , and me ?

resulit = msg.replace('you','')
print(resulit)  # how are  ? i am fine! thank  , and  ?

msg = 'how are you ? i am fine! thank you , and you ?'
resulit = msg.replace('you','+',2)
print(resulit)  # how are + ? i am fine! thank + , and you ?

5.5 字符串.strip()

# 5.字符串.strip() -   去掉字符串前后的空白字符

5.6 isupper()

# 字符串.isupper()   -   判断字符串是否是纯大写字母字符串
# 字符.isupper()  -   判断字符是否是大写字母
print('JSKS'.isupper())
print('A'.isupper())

5.7 字符串.islower()

# 7.字符串.islower()   -   判断字符或者字符串是否全是小写字母
print('a'.islower())        # True

5.8 字符串.isdigit()

# 8.字符串.isdigit()   -   判断字符或者字符串是否全是数字字符
print('9'.isdigit())
print('720233'.isdigit())

5.9 字符串.upper()

# 9.字符串.upper() -   转换成大写字母
print('hs及食宿293jik90dasml'.upper()) # HS及食宿293JIK90DASML

5.10 字符串.lower()

# 10.字符串.lower()    -   转换成小写字母
print('FAhs及食宿293jik90dDADAJKD'.lower())    # fahs及食宿293jik90ddadajkd


char = 's'
if char.islower() or char.isupper():
    print('是字母')
else:
    print('不是字母')

6.FormatString

解决问题:字符串内容不确定

name = '张三'
age = 30
money = 12000

# xxx今年xx岁,月薪:xxxx.xx元!

6.1 字符串拼接

msg = name + '今年' + str(age) + '岁,月薪:' + str(money) + '元!'
print(msg)

6.2 格式字符串 - 包含格式占位符的字符串

"""
语法:包含格式占位符的字符串%(数据1,数据2,数据3...)
注意:()中的数据必须和前面字符串中的占位符一一对应
常见的格式占位符:
%s  -   可以给任何类型的数据占位
%d  -   可以给任何数字占位(整数占位符,如果给的数字是小数,就自动转换成整数在拼接到字符串中)
%f  -   可以给任何数字占位(浮点数占位)
%.Nf    -   (控制保留N位小数   -   默认保留是6位小数)

"""
msg = '%s今年%s岁,月薪:%s元!'%(name,age,money)
print(msg)

result = '%.8f华沙圣诞卡'%(3.1111111)
print(result)

6.3 f - string

"""
在字符串的最前面(引号的前面)加f,就可以在字符串中通过 {表达式} 中表达式的结果来给字符串提供内容
"""
msg = f'{name}今年{age}岁,月薪:{money}元!'
print(msg)      # 张三今年30岁,月薪:12000元!

a = 100
b = 20
# 'xxx + xx = xxx'
print(f'{a} + {b} = {a+b}')     # '100 + 20 = 120'
# {表达式:.Nf}  -  控制小数保留N位小数
a = 1.234
b = 2.3
print(f'{a:.2f} + {b:.2f} = {a + b:.0f}')

# {表达式;.N%}

c = 0.98
print(f'及格率:{c:.0%}')

# {表达式:N%}

d = 4354283
print(f'¥{d:,.2f}') # ¥4,354,283.00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值