Python Programs by Level (1)
Python Programs by Level (1)
LEVEL 1
Length of a string (without len())
s = input("Enter a string: ")
count = 0
for _ in s:
count += 1
print("Length of string:", count)
Character frequency
s = 'google.com'
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
print(freq)
print(first_last_2('w3resource'))
print(first_last_2('w3'))
print(first_last_2(' w'))
print(change_char('restart'))
print(swap_chars('abc', 'xyz'))
LEVEL 2
Add 'ing' or 'ly'
def add_ing(s):
if len(s) < 3:
return s
elif s.endswith("ing"):
return s + "ly"
else:
return s + "ing"
print(add_ing("abc"))
print(add_ing("string"))
print(remove_char("Python", 2))
print(swap_first_last("python"))
LEVEL 3
Remove characters at odd index
def remove_odd_index(s):
return ''.join(s[i] for i in range(len(s)) if i % 2 == 0)
print(remove_odd_index("abcdef"))
Word frequency
sentence = input("Enter a sentence: ")
words = sentence.split()
freq = {}
print(freq)
LEVEL 4
HTML Tag wrapper
def add_tags(tag, text):
return f"<{tag}>{text}</{tag}>"
print(add_tags('i', 'Python'))
print(insert_middle('[[]]', 'Python'))
print(insert_end('Python'))
print(insert_end('Exercises'))
print(first_three('ipy'))
print(first_three('python'))