2.替换空格
题目链接
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy
代码:
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(" ","%20")
知识点:
python String replace() 方法
([]
中的内容为可选内容)
str.replace(old, new[,max])
这里用new
的内容替换old
的内容,如果有max
参数,则只替换从左至右最多max
次,从max+1
次起不再替换
Example:
#!/usr/bin/python
str = "this is string example....wow!!! this is really string"
print str.replace("is", "was")
print str.replace("is", "was", 3)```
Result:
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string