Write a program in python that prompts the user to enter a string. Write the input string and the length of the corresponding string into exercise7_2.txt. The program is saved as exercise7_2.py

First create a new exercise7_2.txt file

Then analyze step by step

It should be noted that str() should be added to the content to be written into the file in f.write(), otherwise an error will be prompted

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


Guess you like

Origin blog.csdn.net/qq_46161529/article/details/121442746