Python Substring Generation with Nested Loops

Day 34 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find all possible substrings of a given string. A substring is a continuous sequence of characters within a string. The goal was to generate every possible substring using nested loops. What the program does: • Takes a string as input • Uses nested loops to generate substrings • Extracts substrings using string slicing • Stores all substrings in a list • Returns the list of all substrings How the logic works: •A function find_all_substrings(s) is defined •An empty list substrings is created to store results •The first loop selects the starting index i •The second loop selects the ending index j •Each substring is extracted using slicing: s[i:j+1] •The substring is appended to the result list •Finally, all substrings are returned Example: Input: "abc" Output: ['a', 'ab', 'abc', 'b', 'bc', 'c'] Another example: Input: "hello" Output: ['h', 'he', 'hel', 'hell', 'hello', 'e', 'el', 'ell', 'ello', 'l', 'll', 'llo', 'l', 'lo', 'o'] Why this approach works well: – Uses nested loops to explore all start–end positions – Demonstrates string slicing clearly – Time Complexity: O(n²) Key learnings from Day 34: – Understanding substring generation – Using nested loops effectively – Applying string slicing in Python – Strengthening string algorithm concepts #100DaysOfCode #Day34 #Python #PythonProgramming #StringAlgorithms #ProblemSolving #CodingPractice #Algorithms #LearnByDoing #ComputerScience #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney

  • text

To view or add a comment, sign in

Explore content categories