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.
Python program to search for a pattern in string
Output:
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