Put all string functions together in one program. You write the code โ only light hints available. The reference card below is your main support.
upper(), lower(), len(), find(), isalpha(), isdigit() in one programinput.txt in the same folder as your Python file.
Python is a versatile language Jersey has a population of 103000 GCSE Computer Science exam is 1CP2 I scored 95 percent on my test the quick brown fox jumps Programming helps solve problems My postcode is JE2 3AB There are 365 days in a year Hello World Channel Islands include Jersey and Guernsey Room 101 is down the corridor abcdefghijklmnopqrstuvwxyz
Write a program that reads every line and writes to output.txt a report showing each line's: original text, length, first character, last character, and an uppercase version. Format it clearly.
output_file.write(f"Line: {line} | Len: {len(line)} | First: {line[0]}\n")Extend your program (or write a new one) that searches each line for a keyword that the user types. Only write lines containing that keyword to output.txt. Print how many matches were found. Make the search case-insensitive.
if keyword.lower() in line.lower():Write a program that reads the file and for each line counts: number of letters, number of digits, number of spaces. Write a summary line to output.txt for each line. At the end print totals across the whole file.
total_letters = 0, then inside the loop add: total_letters += lettersTake your Task 03 code and refactor it to use subroutines. Create at least two functions:
1. def count_chars(line) โ takes a string, returns the count of letters, digits, and spaces
2. def format_report(line, letters, digits, spaces) โ takes the data and returns a formatted string for the output file
return letters, digits, spaces and unpack them: l, d, s = count_chars(line)return f"Line: {line} | Letters: {letters} | Digits: {digits} | Spaces: {spaces}\n"This code has 5 bugs. Find them all โ no clue which lines they're on this time. Click underlined text to fix.
s = "hello"
print(s[1:4].upper())s?" hello ".strip().upper()[0] return?These snippets combine multiple string functions. What does each one print?
def process(text):
return text.strip().upper()[0:5]
print(process(" hello world "))line = "Test123"
count = 0
for ch in line:
if ch.isdigit():
count += 1
print(count)word = "Python"
result = word.lower().find("th")
print(result)def count_chars(text):
letters = 0
digits = 0
for ch in text:
if ch.isalpha(): letters += 1
elif ch.isdigit(): digits += 1
return letters, digits
l, d = count_chars("Hi 42!")
print(f"{l} letters, {d} digits")Write a complete program using at least 3 subroutines that produces a detailed text statistics report. Your program should include:
1. def count_chars(line) โ counts letters, digits, spaces
2. def find_longest_word(line) โ finds the longest word in a line (hint: use .split())
3. def generate_summary(total_lines, total_letters, total_digits, longest_line) โ returns a formatted summary string
Read input.txt and write a full report to output.txt. The report should include per-line stats AND an overall summary at the end with totals and the longest line found.
words = line.split(). Then loop through and compare lengths: if len(word) > len(longest): longest = word