'How to create a statement that will print out words that start with particular word? [closed]

How to print words starting from a particular letter in python without using functions, but using methods or loops.

1 ) I got a string and wants to print words starting with 'm'

St= "where is my mobile"

result = "my", "mobile"

2 ) For the below list, how to output list starting with "p", which can be either lower or upper.

List = ['mobile',"pencil","Pen","eraser","Book"]
 
 RESULT= "pencil","pen".

Thanks

Nb: This is not a homework, only a python newbie



Solution 1:[1]

Try this code:

#String to be splitted
St = 'where is my mobile'

#Split the string on blank characters
List = St.split()

#for each element in the list, if it starts with 'm' then print it
for s in List:
    if s.startswith('m'):
        print(s)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1