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 StringBeginnerReverse a string entered by the user.Open example →py# Program to reverse a string s = input("Enter a string: ")
- 02Check Palindrome StringBeginnerCheck whether a given string is a palindrome (ignoring case and spaces).Open example →py# Program to check if a string is a palindrome s = input("Enter a string: ")
- 03Count Vowels in StringBeginnerCount the number of vowels in a given string.Open example →py# Program to count vowels in a string s = input("Enter a string: ")
- 04Count Consonants in StringBeginnerCount the number of consonants (alphabetic non-vowels) in a string.Open example →py# Program to count consonants in a string s = input("Enter a string: ")
- 05Count Digits in StringBeginnerCount how many digit characters appear in a string.Open example →py# Program to count digits in a string s = input("Enter a string: ")
- 06Remove Special CharactersBeginnerRemove all non-alphanumeric characters from a string.Open example →py# Program to remove special characters from a string s = input("Enter a string: ")
- 07Remove WhitespaceBeginnerRemove all whitespace characters from a string.Open example →py# Program to remove all whitespace from a string s = input("Enter a string: ")
- 08Find SubstringBeginnerCheck if a substring exists within a string and find its index.Open example →py# Program to find substring in a string text = input("Enter main string: ") sub = input("Enter substring: ")
- 09Replace SubstringBeginnerReplace all occurrences of a substring with another substring.Open example →py# Program to replace substring in a string text = input("Enter main string: ") old = input("Enter substring to replace: ")
- 10Character FrequencyBeginnerCount the frequency of each character in a string.Open example →py# Program to count frequency of each character in a string s = input("Enter a string: ")
- 11Capitalize First Letter of Each WordBeginnerCapitalize the first letter of each word in a sentence.Open example →py# Program to capitalize first letter of each word s = input("Enter a sentence: ")
- 12Convert String to Title CaseBeginnerSame as .title() on the previous page, printed in one line. McDonald still becomes Mcdonald.Open example →py# Program to convert string to title case s = input("Enter a sentence: ")
- 13Sort String CharactersBeginnerSort the characters of a string in ascending order.Open example →py# Program to sort characters of a string s = input("Enter a string: ")
- 14Find Longest WordBeginnerFind the longest word in a sentence.Open example →py# Program to find the longest word in a sentence sentence = input("Enter a sentence: ")
- 15Find Shortest WordBeginnerFind the shortest word in a sentence.Open example →py# Program to find the shortest word in a sentence sentence = input("Enter a sentence: ")
- 16String CompressionIntermediateImplement a simple run-length encoding (RLE) compression for strings.Open example →py# Program for simple string compression (run-length encoding) s = input("Enter a string: ")
- 17String RotationIntermediateCheck whether one string is a rotation of another.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 CheckBeginnerCheck whether two strings are anagrams of each other.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 StringsBeginnerSplit a sentence into words and join them with a given delimiter.Open example →py# Program to split and join a string sentence = input("Enter a sentence: ") delimiter = input("Enter delimiter: ")
- 20Count Words in StringBeginnerCount the number of words in a sentence.Open example →py# Program to count words in a sentence sentence = input("Enter a sentence: ")
- 21Extract Numbers from StringIntermediateExtract all integer numbers from a mixed string.Open example →py# Program to extract numbers from a string import re
- 22Validate Email Using RegexIntermediateA toy regex. It accepts test@example.com. It also accepts garbage that looks vaguely like an email.Open example →py# Program to validate email using regex import re
- 23Validate Phone Number Using RegexIntermediateValidate a simple phone number format using regular expressions.Open example →py# Program to validate phone number using regex import re
- 24Check PangramIntermediateCheck whether a sentence is a pangram (contains every letter of the alphabet at least once).Open example →py# Program to check pangram import string
- 25Convert snake_case to camelCaseIntermediateConvert a snake_case string to camelCase.Open example →py# Program to convert snake_case to camelCase snake = input("Enter snake_case string: ")