[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Python Programs by Level (1)

The document contains Python programming exercises organized by difficulty levels. Level 1 includes string manipulation tasks such as counting characters, frequency analysis, and character replacement. Levels 2 and 3 introduce more complex tasks like modifying strings based on conditions, word frequency counting, and unique word sorting, while Level 4 features functions for HTML tagging and string insertion.

Uploaded by

carine.finetta30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python Programs by Level (1)

The document contains Python programming exercises organized by difficulty levels. Level 1 includes string manipulation tasks such as counting characters, frequency analysis, and character replacement. Levels 2 and 3 introduce more complex tasks like modifying strings based on conditions, word frequency counting, and unique word sorting, while Level 4 features functions for HTML tagging and string insertion.

Uploaded by

carine.finetta30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Programs by Level

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)

First 2 and last 2 chars


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'))

Replace occurrences of first char with '$'


def change_char(s):
first = s[0]
return first + s[1:].replace(first, '$')

print(change_char('restart'))

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'))

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"))

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!"))

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"]))

Remove nth index character


def remove_char(s, n):
return s[:n] + s[n+1:]

print(remove_char("Python", 2))

Swap first and last character


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
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 = {}

for word in words:


freq[word] = freq.get(word, 0) + 1

print(freq)

Display input in upper and lower case


text = input("Enter something: ")
print("UPPER:", text.upper())
print("lower:", text.lower())

Unique words in sorted order


words = input("Enter comma-separated words: ").split(',')
unique_sorted = sorted(set(words))
print(",".join(unique_sorted))

LEVEL 4
HTML Tag wrapper
def add_tags(tag, text):
return f"<{tag}>{text}</{tag}>"

print(add_tags('i', 'Python'))

Insert string in the middle


def insert_middle(container, word):
return container[:len(container)//2] + word + container[len(container)//2:]

print(insert_middle('[[]]', 'Python'))

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'))

First three characters


def first_three(s):
return s[:3] if len(s) >= 3 else s

print(first_three('ipy'))
print(first_three('python'))

You might also like