#Write a program to create a text file and print the lines starting with ‘T’ or ‘P’. (Both# uppercase and lowercase)
Question
Write a program to create a text file and print the lines starting with ‘T’ or ‘P’. (Both uppercase and lowercase)
🧐 Not the exact question you are looking for?Go ask a question
Solution
Sure, here is a Python program that creates a text file and prints the lines starting with 'T' or 'P' (both uppercase and lowercase).
# Step 1: Create a text file and write some data into it
with open('test_file.txt', 'w') as f:
f.write('This is a test file.\n')
f.write('Python is fun.\n')
f.write('programming is interesting.\n')
f.write('The end.\n')
f.write('pizza is delicious.\n')
# Step 2: Open the file in read mode and print lines starting with 'T' or 'P'
with open('test_file.txt', 'r') as f:
for line in f:
if line[0] in ['T', 't', 'P', 'p']:
print(line)
This program first creates a text file named 'test_file.txt' and writes some lines into it. Then it opens the file in read mode, reads each line, and checks if the first character of the line is 'T', 't', 'P', or 'p'. If it is, it prints the line.
Similar Questions
0/0
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.