我有以下代码:

url = 'abcdc.com'
print(url.strip('.com'))

我以为是abcdc

我得到:abcd

现在我知道了

url.rsplit('.com', 1)

有没有更好的办法?


参见.strip/.rstrip/。lstrip字符串方法在Python中工作?对于第一次尝试的具体解释。


当前回答

一个更广泛的解决方案,增加替换后缀的可能性(你可以用空字符串替换),并设置替换的最大数量:

def replacesuffix(s,old,new='',limit=1):
    """
    String suffix replace; if the string ends with the suffix given by parameter `old`, such suffix is replaced with the string given by parameter `new`. The number of replacements is limited by parameter `limit`, unless `limit` is negative (meaning no limit).

    :param s: the input string
    :param old: the suffix to be replaced
    :param new: the replacement string. Default value the empty string (suffix is removed without replacement).
    :param limit: the maximum number of replacements allowed. Default value 1.
    :returns: the input string with a certain number (depending on parameter `limit`) of the rightmost occurrences of string given by parameter `old` replaced by string given by parameter `new`
    """
    if s[len(s)-len(old):] == old and limit != 0:
        return replacesuffix(s[:len(s)-len(old)],old,new,limit-1) + new
    else:
        return s

在您的情况下,给定默认参数,将获得所需的结果:

replacesuffix('abcdc.com','.com')
>>> 'abcdc'

一些更普遍的例子:

replacesuffix('whatever-qweqweqwe','qwe','N',2)
>>> 'whatever-qweNN'

replacesuffix('whatever-qweqweqwe','qwe','N',-1)
>>> 'whatever-NNN'

replacesuffix('12.53000','0',' ',-1)
>>> '12.53   '

其他回答

这取决于你对url的了解程度以及你想要做什么。如果你知道它总是以“。com”(或“。net”或“。org”)结尾,那么

 url=url[:-4]

是最快的解决办法。如果它是一个更通用的url,那么你可能会更好地查看python附带的urlparse库。

另一方面,如果你只是想在期末考试后删除所有内容。'在一个字符串中

url.rsplit('.',1)[0]

将工作。或者如果你想把所有的东西都放在第一。那就试试

url.split('.',1)[0]

一个更广泛的解决方案,增加替换后缀的可能性(你可以用空字符串替换),并设置替换的最大数量:

def replacesuffix(s,old,new='',limit=1):
    """
    String suffix replace; if the string ends with the suffix given by parameter `old`, such suffix is replaced with the string given by parameter `new`. The number of replacements is limited by parameter `limit`, unless `limit` is negative (meaning no limit).

    :param s: the input string
    :param old: the suffix to be replaced
    :param new: the replacement string. Default value the empty string (suffix is removed without replacement).
    :param limit: the maximum number of replacements allowed. Default value 1.
    :returns: the input string with a certain number (depending on parameter `limit`) of the rightmost occurrences of string given by parameter `old` replaced by string given by parameter `new`
    """
    if s[len(s)-len(old):] == old and limit != 0:
        return replacesuffix(s[:len(s)-len(old)],old,new,limit-1) + new
    else:
        return s

在您的情况下,给定默认参数,将获得所需的结果:

replacesuffix('abcdc.com','.com')
>>> 'abcdc'

一些更普遍的例子:

replacesuffix('whatever-qweqweqwe','qwe','N',2)
>>> 'whatever-qweNN'

replacesuffix('whatever-qweqweqwe','qwe','N',-1)
>>> 'whatever-NNN'

replacesuffix('12.53000','0',' ',-1)
>>> '12.53   '

如果你确定字符串只出现在末尾,那么最简单的方法是使用'replace':

url = 'abcdc.com'
print(url.replace('.com',''))

使用replace和count

这可能看起来有点hack,但它确保你不使用startwith和if语句进行安全替换,使用replace的count参数你可以限制替换为一个:

mystring = "www.comwww.com"

前缀:

print(mystring.replace("www.","",1))

后缀(把前缀写反了).com变成了moc。

print(mystring[::-1].replace("moc.","",1)[::-1])

我使用了内置的rstrip函数,如下所示:

string = "test.com"
suffix = ".com"
newstring = string.rstrip(suffix)
print(newstring)
test