-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. testing@email.com
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 = 'test@gmail.com'
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'@.*', '', 'test@email.com')
geckos
  • 5,687
  • 1
  • 41
  • 53
0

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

s = test@email.com
end = s.find('@')
s[:end]
  • Rephrase your answer to capture what you mean, s = 'test@email.com' and there should be a print statement at the end of your code block. – oriohac Mar 30 '23 at 06:45
0

Do you want a part of account? ( for example " testing" in "testing@baba.net")

Then

exampleString = "testing@baba.net"
indexOfAt = exampleString.find("@") # get index of @
print (indexOfAt)
accountPart = exampleString[:indexOfAt]
print (accountPart)

the result is as follows

7
testing
김종명
  • 101
  • 7