当前位置: 首页 > web >正文

字符串操作

1.字符串的截取

str1 = 'abcdefg'n = len(str1)
s = str1[5]
s1 = str1[1:6:2]
s2 =  str1[2:]
s3 =  str1[:2]print(n)
print(s)
print(s1)
print(s2)
print(s3)

7
f
bdf
cdefg
ab
[Finished in 0.2s]

2.字符串检索


# 判断字串是否存在,子串出现位置和次数f1 = 'cd' in str1
f2 = 'cd' not in str1
n1 = str1.count('c')# 找不到返回-1
n2 = str1.find('c')# 找不到直接报异常
n3 = str1.index('c')print(f1)
print(f2)
print(n1)
print(n2)
print(n3)

3.字符串训练

name = '倔强-五月天.flac'a = name.find('-')
b = name.find('.')f = name[a+1:b]
s = name[:a]split1 = name[a]
last = name[b:]res = f+split1+s+last
print(res)# f = name[3:6]
# s = name[0:2]# split1 = name[2]# last = name[-5:]# res = f+split1+s+last
# print(res)

五月天-倔强.flac
[Finished in 0.2s]

4.字符串转换

# 完全拆解成列表
str1 = 'abcd'
L = list(str1)# 字符串.split(子串): 子串作为分隔符号,拆解成列表
L1 = 'a-b-c-d'.split('-')
L2 = 'a-b-c-d-'.split('-')# 字符串.join(列表): 使用字符串(可以为空)把列表拼接成一个字符串,
S1 = '_'.join(['1','2','3'])
S2 = '_'.join('abc')print(L)
print(L1)
print(L2)
print(S1)
print(S2)

[‘a’, ‘b’, ‘c’, ‘d’]
[‘a’, ‘b’, ‘c’, ‘d’]
[‘a’, ‘b’, ‘c’, ‘d’, ‘’]
1_2_3
a_b_c
[Finished in 0.2s]

S3 = 'AbcDE'.lower()
S4 = 'AbcDE'.upper()# 最后一个参数是替换个数,如果只有一个就换一个
S5 = 'abcdefg'.replace('ab','gg',2)
print(S3)
print(S4)
print(S5)

abcde
ABCDE
ggcdefg
[Finished in 0.2s]

5.题目训练

在这里插入图片描述
根据特殊性解

with open('出游名单.txt','r',encoding = 'utf-8') as fr:data = fr.read()rows = data.split('\n')res = []for row in rows:row =row.replace('female','女')row = row.replace('male','男')row =row.replace('a','A')row =row.replace('c','C')res.append(row+'\n')with open('出游名单2.txt','a',encoding='utf-8') as fa:fa.writelines(res)

正常解

with open('出游名单.txt','r',encoding = 'utf-8') as fr:data = fr.read()rows = data.split('\n')res = []rows = rows[:-2]for row in rows:list_row = row.split(',')list_row[-1] = list_row[-1].upper()if list_row[1] == 'female':list_row[1] = '女'else:list_row[1] = '男'res.append(','.join(list_row)+'\n')with open('出游名单2.txt','a',encoding='utf-8') as fa:fa.writelines(res)

6.判断字符串中含有字母或着数字或者中文

第一种,直接用a-z A-Z 0-9判断

s = '中abcDD123$$文'
zm = 0
sz = 0
qt = 0
for c in s:if 'a'<=c<='z' or 'A'<=c<='Z':zm += 1elif '0'<=c<='9':sz += 1else:qt += 1
print(zm)
print(sz)
print(qt)

第二种,直接利用API

for c in s:if c.encode().isalpha():zm += 1elif c.isdigit():sz += 1else:qt += 1print(zm)
print(sz)
print(qt)
http://www.xdnf.cn/news/16456.html

相关文章:

  • ESP32实战:5分钟实现PC远程控制LED灯
  • AI Agent笔记--读腾讯技术公众号
  • dify前端应用相关
  • Java中List集合对象去重及按属性去重
  • 学习随想录-- web3学习入门计划
  • Flutter开发实战之路由与导航
  • Flink是如何实现物理分区?
  • 39.Python 中 list.sort() 与 sorted() 的本质区别与最佳实践
  • C语言开发工具Win-TC
  • Python+Selenium+Pytest+POM自动化测试框架封装
  • C++高效实现AI人工智能实例
  • Flutter开发实战之原生平台集成
  • Flutter开发实战之动画与交互设计
  • 06-ES6
  • Ubuntu22.04提示找不到python命令的解决方案
  • Java 注解(Annotation)详解:从基础到实战,彻底掌握元数据驱动开发
  • 微信小程序 自定义带图片弹窗
  • Windows Server容器化应用的资源限制设置
  • 用户中心项目部署上线03
  • 基于FPGA的SPI控制FLASH读写
  • 服务器:数字世界的隐形引擎
  • JavaScript里的string
  • 使用Python实现单词记忆软件
  • Zookeeper的简单了解
  • 兼容性问题记录
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现轮船检测识别(C#代码UI界面版)
  • 【C/C++】Undefined reference: memset_s
  • 港股历史逐笔十档分钟级订单簿行情数据分析
  • 黑屏运维OceanBase数据库的常见案例
  • 【算法】前缀和经典例题