python编写程序,提示用户输入字符串。将所输入的字符串,以及对应字符串的长度写入exercise7_2.txt中。程序保存为exercise7_2.py

首先新建一个exercise7_2.txt文件

然后再一步步分析

要注意的是,f.write()里边要写进文件里边的内容,应该加上str()否则会提示错误

with open('f:\\exercise7_2.txt','r+') as f:
    s=input("请输入一个字符串")
    f.write("输入的字符串是:" + str(s) +"\n"+ "字符串的长度是:" + str(len(s)))
    f.close()


猜你喜欢

转载自blog.csdn.net/qq_46161529/article/details/121442746