2

Right now I have a string in the form [email protected]@zzz.com and I want to strip off the @zzz.com so that it comes out as [email protected].

1

7 Answers 7

3

You can use:

"[email protected]@zzz.com".replace("@zzz.com", "")

If you know it will always be "@zzz.com".

Otherwise, you could try:

data = "[email protected]@zzz.com"
if data.count("@") == 2:
    data = data.rsplit('@', 1)[0]

Or, more generally:

data = "[email protected]@zzz.com@___.com"
if data.count("@") > 1:
    data = data.rsplit('@', data.count("@")-1)[0]

You can learn more about the string methods I have used at Python : String Methods

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. is there any documentation for each of the methods that you use here?
@cfarm54 You can find it in the usual place for documentation: docs.python.org/library/stdtypes.html#string-methods
3
>>> '[email protected]@zzz.com'.rpartition('@')[0]
'[email protected]'

Comments

1
string = "[email protected]@zzz.com"
string = "@".join(string.split("@")[:2])

Simple way to do the job. I don't think it's very safe though.

Comments

0
$ python
Python 2.6.6 (r266:84292, Nov 19 2010, 21:55:12) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.sub('@[^@.]*\.com$', '', '[email protected]@zzz.com')
'[email protected]'

Comments

0

This would work. Please check it.

st='[email protected]@zzz.com'  
print st  
newstr=st[0:st.rfind('@')]  
print newstr

Idea is to use rfind and strip find the @ symbol from the end.

Comments

0
string = "[email protected]@zzz.com"

print string[0:string.rfind('@')]

can help you

Comments

0

Not sure if this code is too much for the task, but here ya go.

data = "[email protected]@zzz.com"

def cleaner(email):
   counter = 0
   result = ''
   for i in data:
      if i == "@":
        counter += 1
      if counter == 2:
        break
      result += i
   return result

data = cleaner(data)

data = '[email protected]'

Just pass the data to the cleaner function. For example: cleaner(data) will return the correct answer.

edit: what gribbler posted is 1000x better than this...lol I am still learning :)

data.rpartition('@')[0]

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.