String Programs
reverse, palindrome, split, regex. Strings are where beginners leak hours.
25 programs · open one, then the next
The matching chapter is Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Reverse a String- 01Reverse a StringBeginnerWalk Python text backward with s[::-1] without editing the original. A beginner slicing lesson includes empty strings and limits of reversing Unicode symbols.Open example →py# Program to reverse a string s = input("Enter a string: ")
- 02Check Palindrome StringBeginnerIgnore whitespace and case before checking a Python text palindrome. Beginners trace normalization and learn why punctuation or empty text changes the answer.Open example →py# Program to check if a string is a palindrome s = input("Enter a string: ")
- 03Count Vowels in StringBeginnerCount vowel occurrences, not just unique letters, with Python sum and a generator. A beginner string scan includes both cases and states its English vowel rule.Open example →py# Program to count vowels in a string s = input("Enter a string: ")
- 04Count Consonants in StringBeginnerExclude punctuation before counting Python consonants. Beginners combine isalpha with vowel exclusion and see where the English-only rule misclassifies text.Open example →py# Program to count consonants in a string s = input("Enter a string: ")
- 05Count Digits in StringBeginnerMeasure digit characters rather than whole numbers in Python text. A beginner isdigit scan explains signs, decimal points, repeated digits, and Unicode surprises.Open example →py# Program to count digits in a string s = input("Enter a string: ")
- 06Remove Special CharactersBeginnerKeep letters, numbers, and whitespace in Python text. Beginners use join and notice why deleting punctuation can merge words instead of separating them.Open example →py# Program to remove special characters from a string s = input("Enter a string: ")
- 07Remove WhitespaceBeginnerRemove Python whitespace from the middle as well as the ends of text. A beginner filter compares isspace with strip and shows why neighboring words merge.Open example →py# Program to remove all whitespace from a string s = input("Enter a string: ")
- 08Find SubstringBeginnerA Python match at index zero is still a match. This beginner str.find walkthrough checks -1 explicitly and explains empty targets and case-sensitive positions.Open example →py# Program to find substring in a string text = input("Enter main string: ") sub = input("Enter substring: ")
- 09Replace SubstringBeginnerReplace Python substring matches and keep the returned text. Beginners inspect immutable strings, non-overlapping matches, absent targets, and empty replacements.Open example →py# Program to replace substring in a string text = input("Enter main string: ") old = input("Enter substring to replace: ")
- 10Character FrequencyBeginnerBuild a Python character frequency table one dictionary update at a time. Beginner steps explain get defaults, repeated keys, and quoted labels for whitespace.Open example →py# Program to count frequency of each character in a string s = input("Enter a string: ")
- 11Capitalize First Letter of Each WordBeginnerStore Python title-case output before printing it. A beginner string-method lesson covers lowercased remaining letters and the surprises in names and apostrophes.Open example →py# Program to capitalize first letter of each word s = input("Enter a sentence: ")
- 12Convert String to Title CaseBeginnerPrint Python title-case text directly without a result variable. Beginners compare expression styles and see why acronyms and names need more careful formatting.Open example →py# Program to convert string to title case s = input("Enter a sentence: ")
- 13Sort String CharactersBeginnerSort Python characters into a list, then join them back into text. New programmers examine duplicates, uppercase ordering, spaces, and unchanged source strings.Open example →py# Program to sort characters of a string s = input("Enter a string: ")
- 14Find Longest WordBeginnerChoose a sentence’s longest token with Python max and key=len. A beginner text exercise checks blank input and explains punctuation and first-winner ties.Open example →py# Program to find the longest word in a sentence sentence = input("Enter a sentence: ")
- 15Find Shortest WordBeginnerFind the shortest Python sentence token by length rather than alphabet. First-year practice uses split, an empty-list guard, and a predictable rule for ties.Open example →py# Program to find the shortest word in a sentence sentence = input("Enter a sentence: ")
- 16String CompressionIntermediateEncode aaabbc as a3b2c1 with a Python run counter. Beginners flush the final group and learn why a simple run-length format is not always smaller or reversible.Open example →py# Program for simple string compression (run-length encoding) s = input("Enter a string: ")
- 17String RotationIntermediatePicture a circular cut to test Python string rotations with doubled text. A beginner membership trick explains equal lengths, zero shifts, and empty strings.Open example →py# Program to check if one string is a rotation of another s1 = input("Enter first string: ") s2 = input("Enter second string: ")
- 18Anagram CheckBeginnerCompare Python anagrams without losing repeated-letter counts. Beginners normalize spaces and case, sort characters, and distinguish this rule from rotation.Open example →py# Program to check anagram strings s1 = input("Enter first string: ").replace(" ", "").lower() s2 = input("Enter second string: ").replace(" ", "").lower()
- 19Split and Join StringsBeginnerChoose your own separator for Python words using split and join. A beginner walkthrough explains collapsed whitespace, empty delimiters, and retained punctuation.Open example →py# Program to split and join a string sentence = input("Enter a sentence: ") delimiter = input("Enter delimiter: ")
- 20Count Words in StringBeginnerDecide what counts as a word before using Python split and len. Beginner examples handle blank text, repeated whitespace, and hyphenated tokens without guessing.Open example →py# Program to count words in a sentence sentence = input("Enter a sentence: ")
- 21Extract Numbers from StringIntermediateExtract digit runs from Python text with a small regex. Beginners keep matched strings and leading zeroes while seeing why decimals and signs need a richer rule.Open example →py# Program to extract numbers from a string import re
- 22Validate Email Using RegexIntermediateRead a Python email regex piece by piece as a beginner string exercise. Understand raw strings and literal dots without treating a match as mailbox verification.Open example →py# Program to validate email using regex import re
- 23Validate Phone Number Using RegexIntermediateMatch an optional plus and 10–15 digits with Python regex. A beginner format check explains bounded repetition and why a match does not establish a real number.Open example →py# Program to validate phone number using regex import re
- 24Check PangramIntermediateRequire all 26 English letters with a Python subset test. Beginners use sets for presence, ignore repeated letters, and allow extras without weakening the rule.Open example →py# Program to check pangram import string
- 25Convert snake_case to camelCaseIntermediateTurn hello_world into helloWorld with Python split and capitalization. A beginner naming exercise traces empty segments, underscores, and changed acronyms.Open example →py# Program to convert snake_case to camelCase snake = input("Enter snake_case string: ")