Knowee
Questions
Features
Study Tools

Substring PositionsImplement Python program to display all positions of a sub string in the given main string Constraints:NAExample:Sample Input-1:this is pythonisSample Output-1:Sub string is at position 3Sub string is at position 6

Question

Substring PositionsImplement Python program to display all positions of a sub string in the given main string Constraints:NAExample:Sample Input-1:this is pythonisSample Output-1:Sub string is at position 3Sub string is at position 6

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step guide on how to implement a Python program to display all positions of a substring in the given main string.

Step 1: Define the main string and the substring.

main_string = "this is pythonis"
sub_string = "is"

Step 2: Initialize an empty list to store the positions of the substring.

positions = []

Step 3: Use a for loop to iterate over the main string. For each iteration, check if the substring is found within the main string starting from the current index.

for i in range(len(main_string)):
    if main_string[i:i+len(sub_string)] == sub_string:
        positions.append(i)

Step 4: Print the positions of the substring in the main string.

for position in positions:
    print("Sub string is at position", position)

When you run this program with the given main string and substring, it will print the positions of the substring in the main string.

This problem has been solved

Similar Questions

Write a program to find the substring of a given string.

What function can you use to get the position of a string or character inside a string?Question 13Select one:a.string_position()b.length()c.len()d.strrpos()

Which of the following functions is used to find the position of the particular substring within a string? Answer choicesSelect only one optionREVISITFINDSEARCHFINDALL

Fill in the blank: In Python, a data professional can use the _____ method to insert specific substrings in a larger string. 1 pointformat()type()print()range()

Write a python program to print the number of occurrences of a substring into a line

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.