Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching characters in two Python strings

Tags:

python

regex

I am trying to print the shared characters between 2 sets of strings in Python, I am doing this with the hopes of actually finding how to do this using nothing but python regular expressions (I don't know regex so this might be a good time to learn it).

So if first_word = "peepa" and second_word = "poopa" I want the return value to be: "pa" since in both variables the characters that are shared are p and a. So far I am following the documentation on how to use the re module, but I can't seem to grasp the basic concepts of this.

Any ideas as to how would I solve this problem?

like image 965
Alex_adl04 Avatar asked Dec 06 '22 00:12

Alex_adl04


1 Answers

This sounds like a problem where you want to find the intersection of characters between the two strings. The quickest way would be to do this:

>>> set(first_word).intersection(second_word)
set(['a', 'p'])

I don't think regular expressions are the right fit for this problem.

like image 143
krock Avatar answered Dec 10 '22 11:12

krock