相关疑难解决方法(0)

如何从Python中删除字符串末尾的子字符串?

我有以下代码:

url = 'abcdc.com'
print(url.strip('.com'))
Run Code Online (Sandbox Code Playgroud)

我期望: abcdc

我有: abcd

现在我做

url.rsplit('.com', 1)
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?

python string

339
推荐指数
12
解决办法
55万
查看次数

为什么str.lstrip会删除一个额外的字符?

Python 2.6 (trunk:66714:66715M, Oct  1 2008, 18:36:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> path = "/Volumes/Users"
>>> path.lstrip('/Volume')
's/Users'
>>> path.lstrip('/Volumes')
'Users'
>>> 
Run Code Online (Sandbox Code Playgroud)

我期待输出path.lstrip('/Volumes')应该是/Users

python string

14
推荐指数
3
解决办法
9386
查看次数

标签 统计

python ×2

string ×2