Python program to search for a pattern in string

Here, we will see a Python program to check if a pattern is present in a string or not. And then print all strings consisting of patterns from the array of string.
Submitted by Shivang Yadav, on February 22, 2021

Python string is data structures that are immutable and are collections of characters in python. Array in python is a collection of data elements stored in consecutive memory locations. An array of strings is an array of multiple strings.

Python program to search for a pattern in string

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


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


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

for c in city:


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 String Programs »

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.