Jon Behnken
Jon Behnken

Reputation: 560

Delete first 3 characters of string in Python

I'm trying to delete up some initial preceding characters in a string in Python 2.7. To be more specific, the string is an mx record that looks like 10 aspmx2.googlemail.com. I need to delete the preceding number (which can be single or double digits) and space character.

Here is the code I've come up with thus far, but I'm stuck

mx_name = "10 aspmx2.googlemail.com"
for i in range(0,3):
    char = mx_name[i]
    if char == "0123456789 ":
        short_mx_name.replace(char, "")

For some reason, the if statement is not working correctly and I fail to see why. Any help would be much appreciated.

Thank you.

Upvotes: 0

Views: 2027

Answers (6)

Adirio
Adirio

Reputation: 5286

The minimum modification to your code would be this:

mx_name = "10 aspmx2.googlemail.com"
short_name = mx_name[:]
for i in range(0,3):
    char = mx_name[i]
    if char in "0123456789 ":
        short_name = short_name.replace(char, "", 1)

Your if was checking if the char WAS 1234567890, not if it was included in that set. Also including the 1 is needed to avoid deelting digits and spaces further in the string.

Upvotes: 0

Chen A.
Chen A.

Reputation: 11280

You should use regex for that; There are plenty of regex answers to this question but if you want a more abstract solution you can use:

m = "10 aspmx2.googlemail.com"
match = re.search('(?:\s)(\w.*@.*\.)', m)
match.group(1)

'aspmx2.googlemail.com'

This pattern will match any email address after the first space.

  • (?:\s) - non capturing space char
  • (\w.*@.*\.) - matches alphanumeric character and the underscore followed by @ and anything after in its own group

This will match 4123 [email protected] or some_text [email protected] etc.

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can use re.sub:

import re
mx_name = "10 aspmx2.googlemail.com"
new_name = re.sub("^\d+\s", '', mx_name)

Output:

'aspmx2.googlemail.com'

Regex explanation:

^:anchor for the expression, forcing it to start its search at the beginning of the string

\d+:finds all digits until a non numeric character (in this case the space) is found.

\s: empty whitespace, must be included in this example so that the substitution also catches the space between the digit and email.

In short, ^\d+\s starts the search at the beginning of the string, finds all proceeding digits, and lastly targets the space to make sure that the regex is not scanning part of the email.

Upvotes: 3

Aaditya Ura
Aaditya Ura

Reputation: 12669

You can use regex :

import re

pattern=r'\b[\d\s]{1,3}\b'

string='10 aspmx2.googlemail.com'

new_string=re.sub(pattern,"",string)
print(new_string)

output:

aspmx2.googlemail.com

with single digit:

string='1 aspmx2.googlemail.com' then output:

aspmx2.googlemail.com

Upvotes: 0

Naveen Vijay
Naveen Vijay

Reputation: 16492

Using split function

mx_name = "10 aspmx2.googlemail.com"
mx_name_url = mx_name.strip().split(' ')[1]
# aspmx2.googlemail.com

Using slice function

mx_name = "10 aspmx2.googlemail.com"
mx_name[3:]
# aspmx2.googlemail.com

Upvotes: 0

Transhuman
Transhuman

Reputation: 3547

mx_name.split()[1]

Output:

'aspmx2.googlemail.com'

Upvotes: 0

Related Questions