# Pattern Searching in Python

In the previous article we saw how String comparison works, in this article we will see how we can deal with a common computer science problem ..we have a large text and then we have a pattern. Our task is to find all occurrences of this pattern within this large text. This is a very common problem and common in various applications. In document processors like MS-Word, e-book readers or text editors we can see a practical implementation of dealing with this problem through the find option. While the code and command line output for these implementations are hidden from us in GUI applications, the code is running a pattern matching algorithm as a background operation.

Let us discuss implementation of a simple way of pattern searching in Python, using the methods that we have discussed so far.

### Implementation of Pattern Searching in Python

We have the text “Man on the roof , gazes at the stars”, and the pattern “gazes”. It should return the index of the position of ‘gazes’ within the string, in this case it would be 15. Suppose the text was “AAAAA”, and the pattern was “AAA”, it would return us the output “0 1 2”.

We can attempt to write the relevant code for this output by using the find( ) function.

```txt=input("Enter Text:")
pat=input("Enter pattern: ")
pos=txt.find(pat)
while pos>=0:
print(pos)
pos=txt.find(pat,pos+1)
```
``````Output:
Enter Text: on the roof , gazes at the stars
Enter pattern: gazes
15``````

First we create provisions for the user to enter both the text and the pattern it should search for within that text input. We can use the find function to return the respective indexes where the patterns can be found and store it in a variable.

Next, we have the loop and we state in the loop that while the position is greater than or equal to zero, print the position and change the position (increased by 1). This repeats until the necessary index is obtained for the pattern.

In the next article we will look at how we can check for palindrome strings in Python.
{"ft_lang_label":"__label__en","ft_lang_prob":0.890485,"math_prob":0.8054544,"size":1984,"snap":"2021-43-2021-49","text_gpt3_token_len":431,"char_repetition_ratio":0.13080809,"word_repetition_ratio":0.017441861,"special_character_ratio":0.21875,"punctuation_ratio":0.09547739,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9659732,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T14:33:47Z\",\"WARC-Record-ID\":\"<urn:uuid:19fed146-3f61-4359-84ec-72bcfab2a821>\",\"Content-Length\":\"76062\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:60595b5c-8ba5-4f57-9343-10c3722e717d>\",\"WARC-Concurrent-To\":\"<urn:uuid:f00e5e90-c6b6-496a-a188-be47f887b904>\",\"WARC-IP-Address\":\"104.21.69.234\",\"WARC-Target-URI\":\"https://codekyro.com/pattern-searching-in-python/\",\"WARC-Payload-Digest\":\"sha1:IEZR6GLBKVTIZ5BST7RPOQLA5EUSLYGJ\",\"WARC-Block-Digest\":\"sha1:KWOQ65KPR6EX7EAH5FJGIMGRNMOSIF6P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363400.19_warc_CC-MAIN-20211207140255-20211207170255-00335.warc.gz\"}"}