Write a Python Program to Write 1 to 100 in a Text File
In this article, we will solve an another interesting question based on Text file handling. According to question we have to write 1 to 100 in a text file.
Question - Write a Python Program to Write 1 to 100 in a Text File
So, without wasting any time let's solve this question. If you like the article content, then share it in your coding groups so that more people can take advantage of the content.
Logic and Explanation
To write 1 to 100 in a text file, first we will open the text file in write mode. In my example, I am taking a text file name "demofile.txt" which is present in the same directory where this python file is present. So, I just written the name of file otherwise you have to write the complete path of the file to open the file. This is the code to open file in read mode.
with open("demofile.txt","w") as f:
Now, as we have to write from 1 to 100, we will iterate x from 1 to 100 using for loop and at each run of loop we will write it in text file.
Here's the code for same
for x in range(1,101):
f.write(str(x) + "\n")
This is the complete code for Python Program to Write 1 to 100 in a Text File.
with open("demofile.txt","w") as f:
for x in range(1,101):
f.write(str(x) + "\n")
So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.
Enjoy Coding!!!
Also Read :-
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.