当前位置: 移动技术网 > IT编程>脚本编程>Python > Python学习笔记之replace()

Python学习笔记之replace()

2019年10月29日  | 移动技术网IT编程  | 我要评论

造梦西游龙王怎么打,valist,笔记本电脑排名

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网