Use isalpha(), isdigit(), and isspace() to inspect strings character-by-character. You write more this time!
isalpha() to check if a string contains only lettersisdigit() to check if a string contains only digitsisspace() to check for whitespacefor loopinput.txt in the same folder as your Python file.
Hello World 123 My phone number is 07700 900123 Python3 is great No digits here at all Room 42 is the answer GCSE grades include 9 8 7 6 5 abc def ghi Test123Test456 Spaces at the start and end Mixed CaSe With 99 Numbers
Type any character or word to see what the three functions return!
See how Python classifies each character in a string. Each box is one character.
Complete this program that counts letters, digits, and spaces in each line.
This code has 4 bugs โ some are logic errors, not just syntax! Think carefully.
Write a program that reads input.txt and writes ONLY lines that contain at least one digit to output.txt. Also print the total count of digits found across ALL lines.
for line in file and an inner for ch in line. Keep a running total outside the outer loop.ch.isdigit() inside the inner loop. Set a flag variable (e.g. has_digit = False) and set it True when you find one.# Define a subroutine to count character types def count_chars(text): letters = 0 digits = 0 spaces = 0 for ch in text: if ch.isalpha(): letters += 1 elif ch.isdigit(): digits += 1 elif ch.isspace(): spaces += 1 return letters, digits, spaces # Define a subroutine to check if a line has digits def has_digits(text): for ch in text: if ch.isdigit(): return True return False # Main program input_file = open("input.txt", "r") output_file = open("output.txt", "w") for line in input_file: line = line.strip() l, d, s = count_chars(line) print(f"{line} โ Letters:{l} Digits:{d} Spaces:{s}") if has_digits(line): output_file.write(line + "\n") input_file.close() output_file.close() print("Done! Lines with digits saved to output.txt")
return letters, digits, spaces โ returns 3 values at oncel, d, s = count_chars(line) โ unpacks the 3 returned values into 3 variableshas_digits() function returns True/False โ useful in an if statement
"abc123".isalpha() return?ch = "7", which returns True?word?" ".isspace() return?What does each snippet print? Think carefully about character types!
text = "abc123"
print(text.isalpha())def has_digits(text):
for ch in text:
if ch.isdigit():
return True
return False
print(has_digits("Hello World"))word = " "
print(word.isspace())
print(word.isalpha())Write a program with a subroutine called validate_password(password) that checks if a password is strong. A strong password must:
โข Be at least 8 characters long (use len())
โข Contain at least one letter (use isalpha())
โข Contain at least one digit (use isdigit())
The subroutine should return True if the password is strong, False if not.
Read passwords from input.txt (one per line) and write only the strong passwords to output.txt. Print how many passed and how many failed.
has_letter = False and has_digit = False. Loop through each character with for ch in password. At the end, return len(password) >= 8 and has_letter and has_digit.