python 将字符串‘https://you.ctrip.com/sight/shenyang155.html'中的.html删掉
时间: 2024-01-16 19:05:18 浏览: 136
html5/css3响应式页面开发总结
可以使用字符串的切片操作来删除指定的子字符串,如下所示:
```python
url = 'https://you.ctrip.com/sight/shenyang155.html'
new_url = url[:-5] # 切片操作,删除最后5个字符(.html)
print(new_url)
```
输出结果为:
```
https://you.ctrip.com/sight/shenyang155
```
另外,也可以使用字符串的`replace()`方法将`.html`替换为空字符串,如下所示:
```python
url = 'https://you.ctrip.com/sight/shenyang155.html'
new_url = url.replace('.html', '') # 将 .html 替换为空字符串
print(new_url)
```
输出结果同样为:
```
https://you.ctrip.com/sight/shenyang155
```
阅读全文