Practice
Modify Random Text
Import random. Create a program that creates a blank text file and writes a random number (in the form of a string) between 1 and 1000 on it. Next, close the file. Next, open the file again (this time read it) and read the text. Assign a variable to that data. Print the variable, then print the
int(variable) * 4
.Hidden Message
Create a program that reads the attached text file (textfile.txt) and writes (appends) 2 newlines and then every 7th word followed by a space onto the text file.
Ex: if the text file contained “hi”, “ho”, “ha”, “hy”, “he”, “hu”, “we”, “everyone” it would write 2 newlines and then ‘hi everyone’
Previous Section
20.2 Text FilesNext Section
20.4 ShelveCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.