-2

I am trying to delete characters after "@" in an email address using python. I found some tips online but it didn't provide the exact solution I am looking for.

The whole email address I'm working on is coming from a parameter value.

paramater = [0]  #contains the email address i.e. [email protected]
mainStr = parameter [0]
newstring = mainStr.replace('@' , ' ') 

Obviously, the above code is not providing me with result I am expecting which is to delete all strings after @.

rob
  • 1
  • 2
  • I learned a tip a while back. If you are printing an array to the console, and you want to delete unnecessary values, then don't delete them, just don't print them. You don't have to delete the characters after `@`, just don't print them. –  Sep 11 '19 at 00:57

4 Answers4

2
in = '[email protected]'
out = s.split('@')[0]

or

out = ''.join(re.findall('(.*)@',s))
Derek Eden
  • 4,403
  • 3
  • 18
  • 31
1

With regex

import re
re.sub(r'@.*', '', '[email protected]')
geckos
  • 5,687
  • 1
  • 41
  • 53
0

I don't know your code. But, I write code by my way.

s = [email protected]
end = s.find('@')
s[:end]
0

Do you want a part of account? ( for example " testing" in "[email protected]")

Then

exampleString = "[email protected]"
indexOfAt = exampleString.find("@") # get index of @
print (indexOfAt)
accountPart = exampleString[:indexOfAt]
print (accountPart)

the result is as follows

7
testing
김종명
  • 101
  • 7