How do I remove a substring from the end of a stri

2019-01-02 19:46发布

I have the following code:

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

I expected: abcdc

I got: abcd

Now I do

url.rsplit('.com', 1)

Is there a better way?

标签: python string
16条回答
梦醉为红颜
2楼-- · 2019-01-02 20:02

url.rsplit('.com', 1)

is not quite right.

What you actually would need to write is

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

, and it looks pretty succinct IMHO.

However, my personal preference is this option because it uses only one parameter:

url.rpartition('.com')[0]
查看更多
春风洒进眼中
3楼-- · 2019-01-02 20:04

In one line:

text if not text.endswith(suffix) or len(suffix) == 0 else text[:-len(suffix)]
查看更多
旧人旧事旧时光
4楼-- · 2019-01-02 20:10

Since it seems like nobody has pointed this on out yet:

url = "www.example.com"
new_url = url[:url.rfind(".")]

This should be more efficient than the methods using split() as no new list object is created, and this solution works for strings with several dots.

查看更多
唯独是你
5楼-- · 2019-01-02 20:12
import re

def rm_suffix(url = 'abcdc.com', suffix='\.com'):
    return(re.sub(suffix+'$', '', url))

I want to repeat this answer as the most expressive way to do it. Of course, the following would take less CPU time

def rm_dotcom(url = 'abcdc.com'):
    return(url[:-4] if url.endswith('.com') else url)

However, if CPU is the bottle neck why write in Python?

When is CPU a bottle neck anyway?? in drivers , maybe.

The advantages of using regular expression is code reusability. What if you next want to remove '.me' , which only has three characters?

Same code would do the trick.

>>> rm_sub('abcdc.me','.me')
'abcdc'
查看更多
宁负流年不负卿
6楼-- · 2019-01-02 20:12
def remove_file_type(infile):
import re
return(re.sub('\.[^.]*$','',infile))
remove_file_type('abc.efg')'abc'
查看更多
残风、尘缘若梦
7楼-- · 2019-01-02 20:18

strip doesn't mean "remove this substring". x.strip(y) treats y as a set of characters and strips any characters in that set from the ends of x.

Instead, you could use endswith and slicing:

url = 'abcdc.com'
if url.endswith('.com'):
    url = url[:-4]

Or using regular expressions:

import re
url = 'abcdc.com'
url = re.sub('\.com$', '', url)
查看更多
登录 后发表回答