Print Substrings that are Prefix of the Given String - Python Last Updated : 31 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The task of printing the substrings that are prefixes of a given string involves generating all the possible substrings that start from the first character of the string and end at any subsequent position.For example, if the given string is s = "hello", the prefixes of the string would include "h", "he", "hel", "hell", and "hello". We want to print these prefixes in order, starting from the smallest to the largest with the entire string being the final prefix.Using SlicingSlicing allows us to extract any part of the string from the start to any given position. This enables us to progressively generate prefixes by increasing the substring length from the start to the full string. Python s = "abdabc" for i in range(1, len(s) + 1): print(s[:i]) Outputa ab abd abda abdab abdabc Table of ContentUsing sliding window Using string comparisonUsing hash set Using sliding window Sliding window extract substrings of different sizes and checks if each substring matches the prefix of the string. It expands the window step by step, performing comparisons for each window size. This method involves slicing and checking substrings at each step. Python s = "ababc" n = 1 # Window size # Loop through all window sizes while n <= len(s): # Check for prefix matches for i in range(len(s) - n + 1): if s[i:i+n] == s[:n]: print(s[i:i+n]) n += 1 # Increase window size Outputa a ab ab aba abab ababc Explanation:while n <= len(s) start a loop that iterates over all possible window sizes from 1 to the length of s .for i in range(len(s) - n + 1) for each window size, slide through the string s to check substrings.if s[i:i+n] == s[:n] compare the current substring to the prefix (first n characters) of the s .print(s[i:i+n]) if a match is found then print the substring.Using string comparisonString comparison finds prefix substrings by slicing the string at each position. It iterates through the string comparing each substring with its corresponding prefix. This approach is simple way to extract and print all prefixes of a string. Python s = "ababc" for i in range(len(s)): if s[:i+1] != s[:i]: print(s[:i+1]) Outputa ab aba abab ababc Explanation:for i in range(len(s)) loop goes through each character in s, starting from the first index.if s[:i+1] != s[:i] checks if the current prefix (s[:i+1]) is different from the previous one (s[:i]) and ensures that duplicates are avoided by only printing when a new prefix appears.print(s[:i+1]) prints the current prefix substring.Using hash set This method uses a hash set to store unique prefixes of a string. As it loops through the string it checks each prefix and prints it only if it hasn’t been seen before. The use of the set ensures that duplicates are avoided efficiently. Python s = "ababc" p = set() # Iterate through the `s` for i in range(1, len(s) + 1): if s[:i] not in p: print(s[:i]) p.add(s[:i]) Outputa ab aba abab ababc Explanation:if s[:i] not in p checks if the current prefix s[:i] is not already in the set p.print(s[:i]) prints the unique prefix.p.add(s[:i]) adds the current prefix to the set p to ensure no duplicates are printed. Comment More infoAdvertise with us Next Article Print Substrings that are Prefix of the Given String - Python R rituraj_jain Follow Improve Article Tags : Technical Scripter Python Python Programs Technical Scripter 2018 prefix python-string substring +3 More Practice Tags : python Similar Reads Python | Get the string after occurrence of given substring The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and 3 min read Python - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element 3 min read Finding Strings with Given Substring in List - Python The task of finding strings with a given substring in a list in Python involves checking whether a specific substring exists within any of the strings in a list. The goal is to efficiently determine if the desired substring is present in any of the elements of the list. For example, given a list a = 3 min read Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python.   Remove the substring from the end of the string using Slicing In 3 min read Python Program to print strings based on the list of prefix Given a Strings List, the task here is to write a python program that can extract all the strings whose prefixes match any one of the custom prefixes given in another list. Input : test_list = ["geeks", "peeks", "meeks", "leeks", "mean"], pref_list = ["ge", "ne", "me", "re"] Output : ['geeks', 'meek 5 min read Python program to find the occurrence of substring in the string Given a list of words, extract all the indices where those words occur in the string. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geeks"] Output : [2, 4] Explanation : best and geeks occur at 2nd and 4th index respectively. Input : test_str = 'geeksforgeeks is 4 min read Python | Get the starting index for all occurrences of given substring Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let's discuss a few methods to solve the given task. Method #1: Using Naive Method Python3 # Python3 code to demonstrate # to find all occurrences of substring in # a 3 min read Python program to split a string by the given list of strings Given a list of strings. The task is to split the string by the given list of strings. Input : test_str = 'geekforgeeksbestforgeeks', sub_list = ["best"] Output : ['geekforgeeks', 'best', 'forgeeks'] Explanation : "best" is extracted as different list element. Input : test_str = 'geekforgeeksbestfor 4 min read Python | Frequency of substring in given string Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in whi 6 min read Python | Count overlapping substring in a given string Given a string and a sub-string, the task is to get the count of overlapping substring from the given string. Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider thi 2 min read Like