Note that there are some explanatory texts on larger screens.

plurals
  1. POSimilar Character's in two strings, return all similar characters (no repetitive characters) problemsetquestion in Python
    primarykey
    data
    text
    <p>In advance, I have searched for a solution to this problem already but there does not seem to be one in existence. So I am currently trying to self teach python by running through the Python "Path" on <a href="http://singpath.appspot.com" rel="nofollow noreferrer">http://singpath.appspot.com</a> and I ran into a problem I do not understand how to solve. I have tried multiple different ways to solve it but keep coming up with different incorrect results. Any hints/help would be spectacular! Thanks in advance!</p> <p>Question:</p> <blockquote> <h3>Comparing Strings</h3> <p>Create a function that takes an input of two strings and returns a string that contains all of the characters that appear in both of the inputs. The result should not have any repeats and should be listed in sorted order. You might want to use the variable lowercase from the string module so that you have all the letters in order.</p> </blockquote> <p>My Current Code:</p> <pre><code>def in_both(s1, s2): s1 = s1.lower() s2 = s2.lower() l1=list(''.join(s1.split())) l2=list(''.join(s2.split())) same = '' for i in l1: if i in l2: same = ''.join(i) return same </code></pre> <p>Example:</p> <pre><code>call: in_both('apple','orange') recieved: 'e' expected: 'ae' </code></pre> <p>As you can see it is only collecting a single similar character. Any suggestions? I've tried a couple of different things but I am unsure where to go from here! Please help!</p> <p>P.s. I got the list idea from a similar question located here: <a href="https://stackoverflow.com/questions/9742832/python-function-to-return-a-list-of-common-letters-in-first-and-last-names">Python Function to return a list of common letters in first and last names</a></p> <p>I understand how it works the problem is I have to return a string with all comparisons at the same time. Thanks 'the wolf' for the idea for the list(s) of characters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload