Python Exercises with Solutions
LEVEL 1: LEVEL 1 Exercises
1. Calculate length of a string (without len())
s = input("Enter a string: ")
count = 0
for _ in s:
count += 1
print("Length of string:", count)
2. Count character frequency in string 'google.com'
s = 'google.com'
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
print(freq)
3. First 2 and last 2 chars of string
def first_last_2(s):
if len(s) < 2:
return ''
elif len(s) == 2:
return s + s
else:
return s[:2] + s[-2:]
print(first_last_2('w3resource'))
print(first_last_2('w3'))
print(first_last_2(' w'))
4. Replace all occurrences of first char (except first) with '$'
def change_char(s):
first = s[0]
return first + s[1:].replace(first, '$')
print(change_char('restart'))
5. Swap first 2 characters of two strings
def swap_chars(s1, s2):
return s2[:2] + s1[2:] + " " + s1[:2] + s2[2:]
print(swap_chars('abc', 'xyz'))
Python Exercises with Solutions
LEVEL 2: LEVEL 2 Exercises
6. Add 'ing' or 'ly' to string
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"))
7. Replace 'not...poor' with 'good'
def not_poor(s):
not_pos = s.find('not')
poor_pos = s.find('poor')
if not_pos != -1 and poor_pos > not_pos:
return s[:not_pos] + 'good' + s[poor_pos+4:]
return s
print(not_poor("The lyrics is not that poor!"))
print(not_poor("The lyrics is poor!"))
8. Length of longest word in a list
def longest_word(words):
max_len = 0
for word in words:
if len(word) > max_len:
max_len = len(word)
return max_len
print(longest_word(["apple", "banana", "watermelon"]))
9. Remove nth index character from string
def remove_char(s, n):
return s[:n] + s[n+1:]
print(remove_char("Python", 2))
10. Swap first and last characters of string
Python Exercises with Solutions
def swap_first_last(s):
if len(s) < 2:
return s
return s[-1] + s[1:-1] + s[0]
print(swap_first_last("python"))
LEVEL 3: LEVEL 3 Exercises
11. 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"))
12. Word frequency in a sentence
sentence = input("Enter a sentence: ")
words = sentence.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
print(freq)
13. Display input in upper and lower case
text = input("Enter something: ")
print("UPPER:", text.upper())
print("lower:", text.lower())
14. Unique comma-separated words sorted
words = input("Enter comma-separated words: ").split(',')
unique_sorted = sorted(set(words))
print(",".join(unique_sorted))
LEVEL 4: LEVEL 4 Exercises
15. HTML tag wrapper
def add_tags(tag, text):
return f"<{tag}>{text}</{tag}>"
print(add_tags('i', 'Python'))
print(add_tags('b', 'Python Tutorial'))
Python Exercises with Solutions
16. Insert string in the middle of another
def insert_middle(container, word):
return container[:len(container)//2] + word + container[len(container)//2:]
print(insert_middle('[[]]', 'Python'))
print(insert_middle('{{}}', 'PHP'))
17. 4 copies of last 2 characters
def insert_end(s):
return s[-2:] * 4 if len(s) >= 2 else s
print(insert_end('Python'))
print(insert_end('Exercises'))
18. First three characters of string
def first_three(s):
return s[:3] if len(s) >= 3 else s
print(first_three('ipy'))
print(first_three('python'))