Reputation: 38920
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]
.
Upvotes: 2
Views: 272
Reputation: 1604
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]
Upvotes: 0
Reputation: 304147
>>> '[email protected]@zzz.com'.rpartition('@')[0]
'[email protected]'
Upvotes: 3
Reputation: 176
string = "[email protected]@zzz.com"
print string[0:string.rfind('@')]
can help you
Upvotes: 0
Reputation: 35619
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
Upvotes: 3
Reputation: 4022
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.
Upvotes: 0
Reputation: 428
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.
Upvotes: 1
Reputation: 11051
$ 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]'
Upvotes: 0