Source Code
import re
def main():
print("Welcome to the Main Function")
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
choice = input("Choice: ")
match choice:
case "E1":
example1()
case "E2":
example2()
case "E3":
example3()
case "E4":
example4()
case "E5":
example5()
case "E6":
example6()
case "E7":
example7()
case "E8":
example8()
case "E9":
example9()
case "E10":
example10()
case "E11":
example11()
case "E12":
example12()
case "Q":
print("Quitting!")
# default case
case _:
print("Invalid, try again!")
# define Example 1 Function
def example1():
print("Example 1")
university = "Kean"
# version 1
print("Index\tCharacter")
for i in range(len(university)):
print(str(i) + "\t" + university[i])
# version 2
for u in university:
print(u)
# first chararacter
print("First Character: " + university[0])
# last character
print("Last Character: " + university[-1])
# length of string
print("Length: " + str(len(university)))
print("Last Character: " + university[len(university)-1])
# define Example 2 Function
def example2():
print("Example 2")
text1 = "a b c d"
words1 = text1.split()
print(words1)
text2 = "apple,banana,cherry,dragonfruit"
words2 = text2.split(',')
print(words2)
# define Example 3 Function
def example3():
print("Example 3")
text = "Hello, World!"
index = text.find("World!")
print(index)
# define Example 4 Function
def example4():
print("Example 4")
sentence = "Repeat, repeat, and repeat again."
search = "repeat"
index = sentence.find(search)
while index != -1:
print("Found at index: " + str(index))
index = sentence.find(search, index + 1)
# define Example 5 Function
def example5():
print("Example 5")
text = "Hello world!"
result = re.match(r"Hello", text)
if result:
print("Match found:", result.group())
else:
print("No match found.")
# define Example 6 Function
def example6():
print("Example 6")
result = re.match(r"world", text)
if result:
print("Match found:", result.group())
else:
print("No match found.")
# define Example 7 Function
def example7():
print("Example 7")
text = "The cat sat on the mat."
result = re.search(r"cat", text)
if result:
print("Match found:", result.group())
else:
print("No match found.")
# define Example 8 Function
def example8():
print("Example 8")
text = "My numbers are 42, 98, and 256."
numbers = re.findall(r"\d+", text)
print(numbers)
# define Example 9 Function
def example9():
print("Example 9")
text = "I like cats. Cats are cute."
new_text = re.sub(r"[Cc]ats", "dogs", text)
print(new_text)
# define Example 10 Function
def example10():
print("Example 10")
text = "apple, orange; banana mango"
fruits = re.split(r"[,; ]+", text)
print(fruits)
# define Example 11 Function
def example11():
print("Example 11")
number = "123-456-7890"
if re.fullmatch(r"\d{3}-\d{3}-\d{4}", number):
print("Valid")
else:
print("Invalid")
# define Example 12 Function
def example12():
print("Example 12")
text = "Contact us at [email protected] or [email protected]."
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text)
print(emails)
main()Last updated