# Python | Intersection of two String

One of the string operations can be computing the intersection of two strings i.e, output the common values that appear in both the strings.

There are various ways in Python, through which we can perform the Intersection of two strings.

Method #1 : Naive Method
Create an empty string and check for new occurrence of character common to both string and appending it. Hence computing the new intersection string. This can be achieved by loops and if/else statements.

 `# Python3 code to demonstrate  ` `# string intersection ` `# using naive method  ` ` `  `# initializing strings ` `test_str1 ``=` `'GeeksforGeeks'` `test_str2 ``=` `'Codefreaks'` ` `  `# using naive method to ` `# get string intersection ` `res ``=` `"" ` `for` `i ``in` `test_str1: ` `    ``if` `i ``in` `test_str2 ``and` `not` `i ``in` `res: ` `        ``res ``+``=` `i ` `         `  `# printing intersection ` `print` `(``"String intersection is : "` `+` `res) `

Output :

```String intersection is : eksfor
```

Method #2 : Using `set() + intersection()`

Firstly, both the strings are converted into sets using `set()` and then intersection is performed using `intersection()`. Returns the sorted set.

 `# Python3 code to demonstrate  ` `# string intersection ` `# using set() + intersection() ` ` `  `# initializing strings ` `test_str1 ``=` `'GeeksforGeeks'` `test_str2 ``=` `'Codefreaks'` ` `  `# using set() + intersection() to ` `# get string intersection ` `res ``=` `set``(test_str1).intersection(test_str2) ` `         `  `# printing intersection ` `print` `(``"String intersection is : "` `+` `str``(res)) `

Output :

```String intersection is : {'e', 'f', 's', 'o', 'k', 'r'}
```

Method #3 : Using `join()`

`join()` performs the task similar to list comprehension in case of lists. This encapsulates whole intersection logic and joins together each element filtered through the intersection logic into one string, hence computing the intersection. It converts the strings into set and then computed & operation on them.

 `# Python3 code to demonstrate  ` `# string intersection ` `# using join() ` ` `  `# initializing strings ` `test_str1 ``=` `'GeeksforGeeks'` `test_str2 ``=` `'Codefreaks'` ` `  `# using join() to ` `# get string intersection ` `res ``=` `''.join(``sorted``(``set``(test_str1) & ` `         ``set``(test_str2), key ``=` `test_str1.index)) ` `         `  `# printing intersection ` `print` `(``"String intersection is : "` `+` `str``(res)) `

Output :

```String intersection is : eksfor
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.666265,"math_prob":0.4305067,"size":3466,"snap":"2020-10-2020-16","text_gpt3_token_len":795,"char_repetition_ratio":0.2351242,"word_repetition_ratio":0.18248175,"special_character_ratio":0.2322562,"punctuation_ratio":0.09751434,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9887143,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T10:50:35Z\",\"WARC-Record-ID\":\"<urn:uuid:1b8644f4-ef1e-4a3e-8d81-160fb59068de>\",\"Content-Length\":\"134569\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:07d52eab-d678-41bb-9d01-ab55989d2056>\",\"WARC-Concurrent-To\":\"<urn:uuid:4dbe82e6-4d23-4b86-a6a2-e893b335b36d>\",\"WARC-IP-Address\":\"168.143.241.153\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-intersection-of-two-string/\",\"WARC-Payload-Digest\":\"sha1:QN626JZ2JG2SSAT3VZZYT7BEWZLYSBCH\",\"WARC-Block-Digest\":\"sha1:EMGM34V5DQHFKO5SQKPBZQWHF4DCZXXP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146341.16_warc_CC-MAIN-20200226084902-20200226114902-00533.warc.gz\"}"}