0

I was wondering if there was a method or way to return letters not in a string.

Meaning: Let's say I have the word "testing". "ing" in "testing" returns true but I want to return the letters in "testing" that are NOT in "ing" --- "test"

My plan was to go in this direction: But clearly it's wrong and I've been stuck on this for a while.

str = "testing"

cmpr = "ing"

    if cmpr in str
      return *letters* not cmpr in string

output = "test"

EDIT Better example: (where the letters do not appear in a row)

str = "testing"

cmpr = "tet"

str.replace(cmpr, '', 1) ----> I want it to return "sing" but it still returns "testing"

Can someone please help me solve this?

5
  • For example "sing" is converted "s"? Commented Sep 19, 2014 at 2:44
  • 1
    What if you have cmpr = 'ab' and str = 'ababac'? Do you want 'abac', 'ac', 'c', or something else? (Also, don't name your strings str, or when you try to call str(something), you'll get a weird TypeError.) Commented Sep 19, 2014 at 2:44
  • in your case I'd want abac Commented Sep 19, 2014 at 2:47
  • @user2456977 Then you'd want str.replace(cmpr, '', 1) to only replace the first occurrence. Commented Sep 19, 2014 at 2:51
  • "testing" --- "tet" ---> I want it to return "sing" but the replace method doesn't do that Commented Sep 19, 2014 at 3:51

3 Answers 3

2

You can try this:

str.replace(cmpr,'')
Sign up to request clarification or add additional context in comments.

1 Comment

"testing" --- "tet" ---> I want it to return "sing" it still returns "testing"
1

Here is the solution:

str = "ababac"
cmpr = "ab"

for chr in cmpr:
    if chr in str:
        str = str.replace(chr,'',1)
print str

Comments

0

Well, if testing is 7 characters long..

0|1|2|3|4|5|6|

T|E|S|T|I|N|G|

You can PRINT the first 6 characters with:

print my_string[0:6]

So I think you could print by the following:(?)

return my_string(0:6)

I don't have Python up and running to test this. It's just where I would start.

To clarify, you could say

return testing(0:3)

So the output should be:

test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.