首页 > 编程语言 > 详细

Python学习笔记之replace()

时间:2019-10-29 11:28:53      阅读:61      评论:0      收藏:0      [点我收藏+]

10-2 可使用方法replace()将字符串中的特定单词都替换为另一个单词。

读取你刚创建的文件learning_python.txt 中的每一行,将其中的Python 都替换为另一门语言的名称,如C。将修改后的各行都打印到屏幕上。

learning_python.txt文件内容如下:

1 In Python you can Handle file
2 In Python you can Automatic operation and maintenance
3 In Python you can make AI

 

编写Python代码:

1 filename=learning_python.txt
2 with open(filename) as file_object:
3     lines = file_object.readlines()
4     for line in lines:
5         print(line.replace(Python, C))

 

执行结果:

1 In C you can Handle file
2 
3 In C you can Automatic operation and maintenance
4 
5 In C you can make AI

 

Python学习笔记之replace()

原文:https://www.cnblogs.com/rainights/p/11757436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!