MODULE – 6 ASSIGNMENT
Regular Expression
1)Write a Python program to check that a string contains only a certain set of characters (in this
case a-z, A-Z and 0-9)
2) Write a Python program to replace all occurrences of space, comma, or dot with a colon.
ANSWERS:
1.
def check_alphanumeric(s):
return s.isalnum()
test_string = "Hello123"
if check_alphanumeric(test_string):
print("The string contains only a-z, A-Z, and 0-9.")
else:
print("The string contains characters outside the range a-z, A-Z, and 0-9.")
The string contains only a-z, A-Z, and 0-9.
2.
def replace_characters(input_string):
translation_table = str.maketrans(characters_to_replace, ':::')
replaced_string = input_string.translate(translation_table) return replaced_string
input_string = "Hello, world. This is a test string."
result = replace_characters(input_string) print(result)
© 360DigiTMG. All Rights Reserved.
Output: Hello::world::This:is:a:test:string:
© 360DigiTMG. All Rights Reserved.