Q:

Python program to search for a pattern in string

belongs to collection: Python String Programs

0

Python program to search for a pattern in string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python program to search for a pattern in string

n=int(input("Enter number of cities : "))

city=()

for i in range(n):
    c=input("Enter City : ")
    city+=(c,)

print(city)

pat=input("Enter Pattern you want to search for? ")

for c in city:
    if(c.find(pat)!=-1):
        print(c)

Output:

Enter number of cities : 5
Enter City : New Delhi
Enter City : New Mumbai
Enter City : Indore
Enter City : Gwalior
Enter City : Chennai
('New Delhi', 'New Mumbai', 'Indore', 'Gwalior', 'Chennai')
Enter Pattern you want to search for? New
New Delhi
New Mumbai

In the above code, we have created an array of strings named city to store names of cities and asked the user to enter names of cities. Then we asked the user to enter a pattern. Using the in method we have check for city names that match the pattern and printed the values.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program for removing i-th character from a ... >>
<< Python program to count occurrence of a word in th...