Reverse a StringProblem StatementGiven a string, return its reversed version.Solutiondef reverse_string(s): return s[::-1]
Check Palindrome StringProblem StatementDetermine if a string reads the same forward and backward.Solutiondef is_palindrome(s): return s == s[::-1]
Count Vowels and ConsonantsProblem StatementCount the number of vowels and consonants in a given string.Solution`def count_vowels_consonants(s): vowels = "aeiouAEIOU" v_count = c_count = 0 for char in s: if char.isalpha(): if char in vowels: v_count += 1 else: c_count += 1 return {"vowels": v_count, "consonants": c_count
Find String Length without len()Problem StatementCalculate the length of a string without using the built-in len() function.Solutiondef find_length(s): count = 0 for _ in s: count += 1 return count
Count Word OccurrencesProblem StatementCount how many times each word appears in a sentence.Solution`def count_words(sentence): words = sentence.split() counts = {
Check AnagramProblem StatementCheck if two strings are anagrams of each other (contain same characters with same frequency).Solutiondef is_anagram(s1, s2): return sorted(s1) == sorted(s2)
Convert to LowercaseProblem StatementConvert all characters in a string to lowercase without using .lower().Solutiondef to_lowercase(s): result = "" for char in s: if 'A' <= char <= 'Z': result += chr(ord(char) + 32) else: result += char return result
Convert to UppercaseProblem StatementConvert all characters in a string to uppercase without using .upper().Solutiondef to_uppercase(s): result = "" for char in s: if 'a' <= char <= 'z': result += chr(ord(char) - 32) else: result += char return result
Remove SpacesProblem StatementRemove all spaces from a given string.Solutiondef remove_spaces(s): return "".join(s.split())
Replace CharacterProblem StatementReplace all occurrences of a character in a string with another character.Solutiondef replace_char(s, old, new): result = "" for char in s: if char == old: result += new else: result += char return result
Find First Non-Repeating CharacterProblem StatementFind the first character in a string that does not repeat.Solution`def first_unique(s): counts = {
Count Characters FrequencyProblem StatementCount the frequency of each character in a string.Solution`def char_frequency(s): freq = {
Check SubstringProblem StatementDetermine if a string contains a specific substring.Solutiondef check_substring(s, sub): return sub in s
Find Longest WordProblem StatementFind the longest word in a given sentence.Solutiondef find_longest_word(sentence): words = sentence.split() if not words: return "" longest = words[0] for word in words: if len(word) > len(longest): longest = word return longest
Remove Duplicate CharactersProblem StatementRemove duplicate characters from a string while maintaining order.Solutiondef remove_duplicates(s): seen = set() result = [] for char in s: if char not in seen: seen.add(char) result.append(char) return "".join(result)
Reverse Words in StringProblem StatementReverse the order of words in a given sentence.Solutiondef reverse_words(sentence): return " ".join(sentence.split()[::-1])
Check Valid Parentheses (Basic)Problem StatementCheck if a string of parentheses (one type only) is balanced.Solutiondef is_balanced(s): count = 0 for char in s: if char == '(': count += 1 elif char == ')': count -= 1 if count < 0: return False return count == 0
Find ASCII ValueProblem StatementFind the ASCII value of a character.Solutiondef get_ascii(char): return ord(char)
Sort CharactersProblem StatementSort the characters of a string alphabetically.Solutiondef sort_string(s): return "".join(sorted(s))
Count Digits in StringProblem StatementCount the total number of numeric digits in a string.Solutiondef count_digits(s): count = 0 for char in s: if char.isdigit(): count += 1 return count
Remove VowelsProblem StatementRemove all vowels from a string.Solutiondef remove_vowels(s): vowels = "aeiouAEIOU" return "".join([char for char in s if char not in vowels])
Check Rotation of StringProblem StatementCheck if one string is a rotation of another.Solutiondef is_rotated_string(s1, s2): if len(s1) != len(s2): return False return s2 in (s1 + s1)
Find Common CharactersProblem StatementFind characters that appear in two strings (intersection).Solutiondef common_characters(s1, s2): return "".join(set(s1) & set(s2))
Count Special CharactersProblem StatementCount symbols and special characters in a string (non-alphanumeric).Solutiondef count_special_chars(s): count = 0 for char in s: if not char.isalnum() and not char.isspace(): count += 1 return count
Capitalize First LetterProblem StatementCapitalize the first letter of each word in a string.Solutiondef capitalize_words(s): return s.title()
Swap Two StringsProblem StatementSwap the contents of two strings (conceptual or using variables).Solutiondef swap_strings(s1, s2): return s2, s1
Find Substring IndexProblem StatementFind the starting index of a substring or -1 if not found.Solutiondef find_substring(s, sub): return s.find(sub)
Check PangramProblem StatementCheck if a string contains every letter of the alphabet at least once.Solutiondef is_pangram(s): letters = set() for char in s.lower(): if 'a' <= char <= 'z': letters.add(char) return len(letters) == 26
Remove PunctuationProblem StatementRemove all punctuation from a string.Solutionimport string def remove_punctuation(s): return "".join([char for char in s if char not in string.punctuation])
Find Repeated WordsProblem StatementFind all words that appear more than once in a sentence.Solution`def repeated_words(sentence): words = sentence.split() counts = {