Python Practice › String Programs
Python String Programs with Solutions
106 string programs that cover slicing, searching, counting, replacing, splitting and joining text. They are among the most common Python interview questions, and each one has an explained solution with real output.
Before you start
Read the Strings & Methods and String Formatting & f-strings lesson first if the topic is new to you.
All programs
- #1First and last 2 characters
Write a Python program to build a string from the first 2 and the last 2 characters of a string. If the string is shorter than 2 characters, return an empty string.Easy - #2Length of the longest string in a list
Write a Python program that returns the length of the longest string in a list.Easy - #3Four copies of the last two characters
Write a Python program to make a string of 4 copies of the last two characters of a string (length must be at least 2).Easy - #4Reverse a string if its length is a multiple of 4
Write a Python program to reverse a string if its length is a multiple of 4; otherwise return it unchanged.Easy - #5Count occurrences of a substring
Write a Python program to count how many times a substring occurs in a string.Easy - #6Vowel or consonant
Write a Python program to test whether a letter is a vowel or a consonant.Easy - #7Longest and shortest word
Write a Python program to find the longest and the shortest word in a string.Easy - #8Longest run of a repeated character
Write a Python program to find the character that repeats most times in a row (consecutively) in a string.Medium - #9String length with a loop
Write a Python program to calculate the length of a string with a loop instead oflen().Easy - #10Replace the second occurrence of each character
Write a Python program to replace the second occurrence of every character with “$”.Medium - #11Swap the first and last characters
Write a Python program to swap the first and last characters of a string.Easy - #12Swap first and last letters of each word
Write a Python program to exchange the first and last characters of every word in a string.Easy - #13Vowel count of each word
Write a Python program to count the vowels in each word and show the result as a dictionary.Easy - #14Repeat vowels 3 times and consonants 2 times
Write a Python program that repeats each vowel 3 times and each consonant 2 times, leaving spaces alone.Medium - #15Reverse the order of words
Write a Python program to rearrange a sentence so its words appear in reverse order.Easy - #16Extract all numbers from a string
Write a Python program to get all the numbers from a multi-line string.Easy - #17Replace Java with Python, any case
Write a Python program to replace the word “Java” (in any letter case) with “Python”.Easy - #18Palindrome words in a string
Write a Python program to get all palindrome words from a string.Easy - #19Build a sentence from a list of words
Write a Python program to create a string from a list of words.Easy - #20Remove duplicate words
Write a Python program to remove duplicate words (ignoring case) and keep the first occurrence.Easy - #21Remove unwanted characters
Write a Python program to remove every character that is not a letter or digit.Easy - #22Longest all-capital word
Write a Python program to find the longest word written in capital letters.Easy - #23Common words in two strings
Write a Python program to find the words that appear in both strings, ignoring punctuation.Easy - #24Smallest and largest word
Write a Python program to find the smallest and largest word in a string.Easy - #25Check a palindrome string
Check whether a given string is a palindrome.Easy - #26Reverse each word
Write a Python program to reverse each word of a string, keeping the word order.Easy - #27Length of a string
Write a Python program to calculate the length of a string.Easy - #28Frequency of each character
Write a Python program to count how often each character appears in a string.Easy - #29Combine two strings
Write a Python program to combine two strings into one.Easy - #30Characters at even indexes
Write a Python program to print the characters at even index positions (0, 2, 4, …).Easy - #31Does a string contain a number?
Write a Python program to check whether a string contains a digit.Easy - #32Count vowels
Write a Python program to count the vowels in a string.Easy - #33Count consonants
Write a Python program to count the consonants in a string.Easy - #34Characters at odd indexes
Write a Python program to print the characters at odd index positions.Easy - #35Remove duplicate characters
Write a Python program to remove duplicate characters, keeping the first occurrence of each.Easy - #36Does a string have special characters?
Write a Python program to check whether a string contains special characters.Easy - #37Swap the first and last letters of a string
Write a Python program to exchange the first and last letters of a string.Easy - #38Convert to uppercase
Write a Python program to convert all the characters in a string to uppercase.Easy - #39Remove newlines from a string
Write a Python program to remove newline characters from a string.Easy - #40Split and join a string
Write a Python program to split a string into words and join them with “-“.Easy - #41Float to 3 decimal places as a string
Write a Python program to format a float to 3 decimal places and convert it to a string.Easy - #42Number words to digits
Write a Python program to convert number words into a number.Easy - #43Position of a word in a sentence
Write a Python program to find the position (word number, starting at 1) of a word in a sentence.Easy - #44Count occurrences of a word
Write a Python program to count how many times a whole word occurs in a string.Easy - #45Least frequent character
Write a Python program to find the least frequent character in a string.Easy - #46Words longer than n characters
Write a Python program to find the words longer than a given length.Easy - #47First 4 characters
Write a Python program to get the first 4 characters of a string.Easy - #48First 2 and last 2 characters
Write a Python program to build a string from the first 2 and last 2 characters.Easy - #49Mirror image of a string
Write a Python program to print the mirror image (reverse) of a string.Easy - #50Split a string on vowels
Write a Python program to split a string wherever a vowel appears.Easy - #51Replace several words at once
Write a Python program to replace several words using a dictionary of replacements.Easy - #52Replace several characters at once
Write a Python program to replace a with 1, t with 2 and o with 3 in one step.Easy - #53Remove blank strings from a list
Write a Python program to remove blank strings from a list of strings.Easy - #54Remove punctuation
Write a Python program to remove punctuation from a string.Easy - #55Find duplicate characters
Write a Python program to find the characters that appear more than once (ignoring spaces).Easy - #56Is one string a subset of another?
Write a Python program to check whether all characters of one string appear in another.Easy - #57Sort the characters of a string
Write a Python program to sort the characters of a string.Easy - #58Random binary string
Write a Python program to generate a random binary string of a given length.Easy - #59Is a substring present?
Write a Python program to check whether a substring is present in a string.Easy - #60Frequency of every substring
Write a Python program to count every substring of a string.Medium - #61Index of a character
Write a Python program to print the index of a character in a string.Easy - #62Strip spaces
Write a Python program to remove leading and trailing spaces.Easy - #63Does a string contain every letter?
Write a Python program to check whether a string contains all letters of the alphabet (a pangram).Easy - #64String to a list of words
Write a Python program to convert a string into a list of words.Easy - #65Swap commas and dots
Write a Python program to swap commas and dots.Easy - #66Count and show vowels
Write a Python program to count and display the vowels in a string.Easy - #67Split on the last delimiter
Write a Python program to split a string on the last occurrence of a delimiter.Easy - #68First repeated word
Write a Python program to find the first word that repeats.Easy - #69Second most repeated word
Write a Python program to find the second most repeated word.Easy - #70Remove all spaces
Write a Python program to remove all spaces from a string.Easy - #71Capitalise the first and last letter of each word
Write a Python program to capitalise the first and last letters of each word.Easy - #72Sum of digits in a string
Write a Python program to add up the digits in a string.Easy - #73Remove leading zeros from an IP address
Write a Python program to remove leading zeros from each part of an IP address.Easy - #74Longest run of zeros
Write a Python program to find the longest run of consecutive 0s in a binary string.Easy - #75Remove consecutive duplicates
Write a Python program to collapse consecutive duplicate characters into one.Easy - #76Characters that occur more than once
Write a Python program to build a string of the characters that occur more than once.Easy - #77Uncommon characters of two strings
Write a Python program to build a string from the characters that appear in only one of two strings.Easy - #78Keep only one given character
Write a Python program to remove all characters except a given one.Easy - #79Count uppercase, lowercase, digits and specials
Write a Python program to count the uppercase, lowercase, special and numeric characters in a string.Easy - #80Number of non-empty substrings
Write a Python program to count the non-empty substrings of a string.Easy - #81Remove symbols and capitalise
Write a Python program to remove unwanted characters from a string and capitalise it.Easy - #82Similarity of two strings
Write a Python program to measure how similar two strings are.Medium - #83Extract numbers as integers
Write a Python program to extract the numbers from a string.Easy - #84Split a multi-line string into lines
Write a Python program to split a multi-line string into a list of lines.Easy - #85Add two numeric strings
Write a Python program to add two strings as numbers and return a string.Easy - #86Name from an email address
Write a Python program to extract the name part of an email address (without trailing digits).Easy - #87Leap years in a range given as text
Write a Python program to count the leap years in a range given as a string like “1981-2001”.Easy - #88Space before each capital letter
Write a Python program to insert a space before every capital letter.Easy - #89Uppercase the second half
Write a Python program to convert the second half of a string to uppercase.Easy - #90Join words with hyphens
Write a Python program to split a string and join it with “-“.Easy - #91Permutations of a string
Write a Python program to list all permutations of a string with a built-in function.Easy - #92Length without spaces
Write a Python program to get the length of a string, not counting spaces.Easy - #93Accept only vowel strings
Write a Python program to accept a string only if it contains vowels only.Easy - #94Remove the kth character
Write a Python program to remove the character at index k.Easy - #95Is a string binary?
Write a Python program to check whether a string contains only 0 and 1.Easy - #96Add "ing" to a string
Write a Python program to add “ing” at the end of a string.Easy - #97Add "ly" after "ing"
Write a Python program to add “ly” if a string ends with “ing”, otherwise add “ing”.Easy - #98Reverse the words
Write a Python program to reverse the order of words.Easy - #99Index of each character
Write a Python program to print the index of each character in a string.Easy - #100First repeated character and its index
Write a Python program to find the first character that repeats and the index of its first occurrence.Easy - #101Swap case
Write a Python program to swap upper and lower case.Easy - #102Collapse repeated characters
Write a Python program to replace each group of repeated characters with a single character.Easy - #103Print a string 3 times
Write a Python program to print a string 3 times.Easy - #104Each character on a new line
Write a Python program to print each character on its own line.Easy - #105Extract email addresses
Write a Python program to extract all email addresses from a string.Easy - #106Extract 10-digit mobile numbers
Write a Python program to get all 10-digit mobile numbers from a string.Easy