【问题标题】:python how to remove http from a string using replace [duplicate]python如何使用replace从字符串中删除http [重复]
【发布时间】:2014-04-05 04:21:53
【问题描述】:

我想删除包含网址的字符串中的“http://”,即:“http://www.google.com”。我的代码是:

import os
s = 'http://www.google.com'
s.replace("http://","")
print s

我尝试将 http:// 替换为空格,但不知何故它仍然打印出 http://www.google.com

我是否在这里错误地使用了替换?谢谢你的回答。

【问题讨论】:

  • 你也可以试试:s.split('//')[1:] or s[7:]

标签: python regex string


【解决方案1】:

字符串是不可变的。这意味着他们的方法都没有更改现有的字符串 - 相反,他们给你一个新的。因此,您需要将结果分配回一个变量(相同或不同):

s = 'http://www.google.com'
s = s.replace("http://","")
print s

【讨论】:

  • 天哪,我太笨了……非常感谢
猜你喜欢
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多