当前位置:网站首页>学习总结week2_5
学习总结week2_5
2022-08-05 05:13:00 【非鱼丶丶】
python容器型数据–字符串
一、字符串相关方法 - 字符串.xxx()
1.字符串.join(序列)
将序列中的元素用指定的字符串拼接在一起,形成一个新的字符串(序列中的元素必须是字符串)
如果需要join的序列中的元素不是字符串,就把他变成字符串
list1 = ['ww', 'wdaf', 'dwadfb']
res = '+'.join(list1)
print(res)
2.字符串1.split(字符串2)
将字符串1中所有的字符串2作为切割点对字符串进行切割
字符串1.split(字符串2, N) - 将字符串1中前N个字符串2作为切割点对字符串进行切割
str1 = 'abcmn123mn448mn'
str2 = 'mn'
num = str1.split(str2, 2)
print(num)
3.字符串1.replace(字符串2, 字符串3,)
将字符串1中所有字符串2都替换成字符串3
字符串1.replace(字符串2, 字符串3, N) - 将字符串1中前N个字符串2都替换成字符串3
res = str1.replace('mn', 'xy')
print(res)
res = str1.replace('mn', '')
print(res)
4.字符串.strip()
删除字符串左右两端的空白字符(‘也可以删括号里的字符串’)
str1 = '//小明'
result = str1.strip('/')
print(result)
5.find、index - 在一个字符串1中查找另外一个字符串2的位置
字符串1.find(字符串2, 开始下标, 结束下标) - 获取字符串1中指定范围内字符串2第一次出现的位置
str1 = 'abcmn123mn448mn'
print(str1.find('m')) # 获取字符串1中字符串2第一次出现的位置(0开始的下标返回)
str1.index('m')
6.其他方法
center、rjust、ljust、zfill
str1 = 'abc'
print(str1.center(8, '+'))
print(str1.rjust(7, '+'))
print(str1.ljust(7, '+'))
print(str1.zfill(7))
str1.count(‘c’) - 计算数量
str1 = 'mnabcmnmn123mnkplmn##mn'
print(str1.count('a')) # 1
print(str1.count('mn'))
str1.endswith(‘c’) - 是否是以c结尾的,输出True/False
str1 = 'mnabcmnmn123mnkplmn##mn'
print(str1.endswith('abc'))
print(str1.endswith('#mn'))
print(str1.startswith('ab'))
print(str1.startswith('mn'))
print('2323'.isdecimal()) # True
print('23211'.isdigit()) # True
print('2323'.isnumeric()) # True
print('2323一十百万Ⅱ拾Ⅲ'.isnumeric()) # True
str1 = '722实ss际上Mn飞机22-2233=+集H合289'
for x in str1:
# '0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'
if x.isdigit() or x.isupper() or x.islower():
print(x)
print('hjas'.islower())
print('JKSS'.isupper())
print('a'.upper())
print('M'.lower())
maketrans、translate
str1 = '1727283112闪烁2nsnk2'
# 创建一个映射表
table = str.maketrans('1234567890', '一二三四五六七八九零') # 1-一;2-二;3-三;...
# 根据映射表的对应的关系替换字符串中的相关字符
result = str1.translate(table)
print(result)
# 星期1 -> 星期一
# 星期7 -> 星期天
str1 = '星期7'
table = str.maketrans('1234567', '一二三四五六天')
print(str1.translate(table))
二、格式字符串
语法:
包含一个或者多个格式占位符的字符串 % (数据1, 数据2, 数据3,…)
说明:()中的数据必须和前面字符串中的占位符一一对应。如果只需要一个数据,那么()可以省略
常用的字符串占位符:
%s - 可以给任何类型的数据占位(字符串占位符)
%d - 只能给数字数据占位(整数占位符)
%f - 只能给数字数据占位(浮点数占位符, 默认保留6位小数)
%.Nf - 只能给数字数据占位(保留N位小数)
name = '小花'
age = 25
money = 15000
message = '%s今年%d岁!月薪:%.2f元' % (name, age, money)
print(message)
result = '%s%s%s-%s' % ('abc', 12, [10, 20], 3.456)
print(result) # 'abc12[10, 20]'
result = '%d-%d' % (12, 2.34)
print(result)
result = '%f-%f' % (2.3, 34)
print(result)
result = '%.2f-%.1f' % (2.3, 34)
print(result)
1.f-string
f-string的基本用法:如果在字符串的最外面加了f,就可以在字符串中通过{}来给字符串提供内容:{表达式} - 表达式的结果就是对应字符串内容
name = '小明'
age = 18
money = 15000
str1 = '姓名:{name}, 年龄:{age}'
print(str1)
str2 = f'姓名:{
name}, 年龄:{
age}, {
name[-1]}'
print(str2)
message = f'{
name}今年{
age}岁,月薪{
money}元 '
print(message)
2.f-string的参数:{提供数据的表达式:参数}
控制小数位数 – {提供数据的表达式.Nf}
res = f'余额:{
money:.2f}'
print(res)
3.大金额逗号分隔
money = 15000481351561
res = f'金额:{
money:,.2f}元'
print(res)
4.小数显示成百分百:{表达式:.N%}
scale = 0.84
res = f'及格率:{
scale:.0%}'
print(res)
5.控制填充长度,实现center、rjust、ljust、zfill的功能
num = 1
class_name = 'py2204'
id1 = f'{
class_name}{
num:0>3}'
print(id1)
res = f'{
num:0<7}'
print(res)
res = f'{
num:^7}'
print(res)
res = f'{
num:*<7}'
print(res)
边栏推荐
- 判断语句_switch与case
- 【Untitled】
- redis cache clearing strategy
- 重新审视分布式系统:永远不会有完美的一致性方案……
- MySQL Foundation (1) - Basic Cognition and Operation
- [Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
- RL强化学习总结(一)
- Matplotlib(一)—— 基础
- CAP+BASE
- 位运算符与逻辑运算符的区别
猜你喜欢
类的底层机制
[Go through 7] Notes from the first section of the fully connected neural network video
第二讲 Linear Model 线性模型
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
Using QR codes to solve fixed asset management challenges
coppercam primer [6]
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
Flutter真机运行及模拟器运行
Flex layout frog game clearance strategy
The mall background management system based on Web design and implementation
随机推荐
"Recursion" recursion concept and typical examples
day11-函数作业
jvm three heap and stack
结构光三维重建(一)条纹结构光三维重建
entry point injection
day10-字符串作业
RL reinforcement learning summary (1)
The mall background management system based on Web design and implementation
human weakness
Distributed systems revisited: there will never be a perfect consistency scheme...
软件设计 实验四 桥接模式实验
02.01-----参数的引用的作用“ & ”
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
『递归』递归概念与典型实例
学习总结week3_4类与对象
Reverse theory knowledge 4
Algorithms - ones and zeros (Kotlin)
Database experiment five backup and recovery
[cesium] element highlighting
Redis - 13. Development Specifications