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 6Sample Input-2:1234 3234 6534 7834345Sample Output-2:Sub string not foundExplanation:this is python ---------> String you want to enteris    -------------> substring you want to search forOutput:Sub string is at position 3   --------> Position where substring is presentSub string is at position 6   ---------> Position where substring is presentPublic Test Cases:# INPUT EXPECTED OUTPUT1 this is pythonisSub string is at position 3 Sub 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 6Sample Input-2:1234 3234 6534 7834345Sample Output-2:Sub string not foundExplanation:this is python ---------> String you want to enteris    -------------> substring you want to search forOutput:Sub string is at position 3   --------> Position where substring is presentSub string is at position 6   ---------> Position where substring is presentPublic Test Cases:# INPUT EXPECTED OUTPUT1 this is pythonisSub string is at position 3 Sub string is at position 6

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

Solution 1

Here is a Python program that implements the task:

def substring_positions(main_string, sub_string):
    positions = [i for i in range(len(main_string)) if main_string.startswith(sub_string, i)]
    if positions:
        for pos in positions:
            print(f"Sub string is at position {pos}")
    else:
        print("Sub string not found")

# Test the function
substring_positions("this is pythonis", "is")
substring_positions("1234 3234 6534 7834345", "is")

This program defines a function substring_positions that takes two arguments: main_string and sub_string. It uses a list comprehension to find all positions in main_string where sub_string starts. If it finds any positions, it prints them. Otherwise, it prints "Sub string not found".

The function is then tested with two examples. The first example is "this is pythonis" with the substring "is", and the second example is "1234 3234 6534 7834345" with the substring "is".

This problem has been solved

Solution 2

Here is a Python program that implements the task:

def substring_positions(main_string, sub_string):
    positions = [i for i in range(len(main_string)) if main_string.startswith(sub_string, i)]
    if positions:
        for pos in positions:
            print(f"Sub string is at position {pos}")
    else:
        print("Sub string not found")

# Test the function
substring_positions("this is pythonis", "is")
substring_positions("1234 3234 6534 7834345", "is")

This program defines a function substring_positions that takes two arguments: main_string and sub_string. It uses a list comprehension to find all positions in main_string where sub_string starts. If it finds any positions, it prints them; otherwise, it prints "Sub string not found".

The function is then tested with two examples. The first example is "this is pythonis" with the substring "is", and the second example is "1234 3234 6534 7834345" with the substring "is".

This problem has been solved

Solution 3

Here is a Python program that implements the task:

def substring_positions(main_string, sub_string):
    positions = [i for i in range(len(main_string)) if main_string.startswith(sub_string, i)]
    if positions:
        for pos in positions:
            print(f"Sub string is at position {pos}")
    else:
        print("Sub string not found")

# Test the function
substring_positions("this is pythonis", "is")
substring_positions("1234 3234 6534 7834345", "is")

This program defines a function substring_positions that takes two arguments: main_string and sub_string. It uses a list comprehension to find all positions in main_string where sub_string starts. If the list positions is not empty, it prints all positions where the substring is found. Otherwise, it prints "Sub string not found".

The function is then tested with two examples. The first example is "this is pythonis" with the substring "is", and the second example is "1234 3234 6534 7834345" with the substring "is".

This problem has been solved

Similar Questions

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

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()

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

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()

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

1/3

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.