PyForge ยท Python practice library
All topics

Topic 05 of 17

Strings

Work with text: slicing, formatting, and the most useful string methods.

Indexing & slicing
f-strings
Methods (upper, split, join, replace)
Immutability

Basic

2 programs

Program 1

String basics

Basic

Length, upper, lower, slicing.

python
s = "Python Programming"
print(len(s))
print(s.upper())
print(s.lower())
print(s[0:6])
print(s[::-1])  # reverse

Program 2

Count vowels

Basic

Count vowels in a sentence.

python
text = "Beautiful is better than ugly"
count = sum(1 for ch in text.lower() if ch in "aeiou")
print("Vowels:", count)

Intermediate

1 program

Program 1

Check palindrome

Intermediate

Compare a string to its reverse.

python
def is_palindrome(s: str) -> bool:
    s = s.lower().replace(" ", "")
    return s == s[::-1]

print(is_palindrome("Race car"))
print(is_palindrome("Hello"))

Advanced

2 programs

Program 1

Word frequency

Advanced

Count occurrences of each word.

python
from collections import Counter

text = "the quick brown fox jumps over the lazy dog the fox"
freq = Counter(text.split())
print(freq.most_common(3))

Program 2

Anagram check

Advanced

Two words are anagrams if sorted letters match.

python
def is_anagram(a, b):
    return sorted(a.lower()) == sorted(b.lower())

print(is_anagram("listen", "silent"))
print(is_anagram("hello", "world"))