0

I have a little "problem" that I would like to solve via programming a simple script. I don't have much programming experience and thought I'd ask here for help for what I should look for or know to do this.

Basically I want to take an email address such as placeholder.1234@fakemail.com and replace it into pl*************4@fakemail.com.

I need the script to take the letters after the first two, and before the last, and turn those letters into asterisks, and they have to match the amount of characters.

Just for clarification, I am not asking for someone to write this for me, I just need some guidance for how to go about this. I would also like to do this in Python, as I already have Python setup on my PC.

Democlient
  • 27
  • 5
  • 2
    Welcome to Stack Overflow. Exactly what is the difficulty? Please read [ask] and https://meta.stackoverflow.com/questions/284236/. Are you able to write code that does *anything* with strings? Can you write code that figures out how many asterisks there should be? Can you write code that figures out what should come before and after the asterisks? If you have those pieces, what prevents you from solving the problem? – Karl Knechtel Feb 26 '22 at 23:44

3 Answers3

3

You can use Python string slicing:

email = "placeholder.1234@fakemail.com"
idx1 = 2
idx2 = email.index("@") - 1
print(email[:idx1] + "*" * (idx2 - idx1) + email[idx2:])

Output:

pl*************4@fakemail.com

Explanation:

  1. Define the string that will contain the email address:
email = "placeholder.1234@fakemail.com"
  1. Define the index of where the asterisks should begin, which is 2:
idx = 2
  1. Define the index of where the asterisks should end, which is the index of where the @ symbol is minus 1:
idx2 = email.index("@") - 1
  1. Finally, using the indices defined, you can slice and concatenate the string defined accordingly:
print(email[:idx1] + "*" * (idx2 - idx1) + email[idx2:])
Red
  • 26,798
  • 7
  • 36
  • 58
  • Wow really appreciate the quick and detailed response. is the first line: email = "placeholder.1234@fakemail.com" Technically a variable? If so, could I make an input system so I can quickly input any email and have it give the desired output? Sorry for formatting, haven't really used this site before. – Democlient Feb 26 '22 at 23:57
  • @Democlient That's right, you can replace it with an `input()`. – Red Feb 26 '22 at 23:58
1

So this email will be a string.

Try use a combination of String indexing and (string replacement or string concatenation).

GooJ
  • 257
  • 3
  • 13
1

First, let's think about what data type we would store this in. It should be a String since it contains letters and characters.

We know we want to replace a portion of the String from the THIRD character to the character 2 before "@".

Let's think about this in terms of indexing now. The third character, or our start index for replacement is at index 2. To find the index of the character "@", we can use the: index() function as so:

end = email.index('@')

However, we want to start replacing from 2 before that index, so we can just subtract 2 from it.

Now we have the indexes (start and endpoint) of what we want to replace. We can now use a substring that goes from the starting to ending indexes and use the .replace() function to replace this substring in our email with a bunch of *'s.

To determine how many *'s we need, we can find the difference in indexes and add 1 to get the total number. So we could do something like:

stars = "*" * (end - start + 1)

email = email.replace(email[start:end + 1], stars)

Notice how I did start:end + 1 for the substring. This is because we want to include that ending index, and the substring function on its own will not include the ending index.

I hope this helped answer your question! Please let me know if you need any further clarification or details:)

Aniketh Malyala
  • 2,650
  • 1
  • 5
  • 14