题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Solution:
def replaceSpace(self, s):
ans = s
for i in range(len(s)):
if s[i].isspace():
ans = ans.replace(s[i], '%20')
return ans
while True:
try:
while True:
S = Solution()
s = raw_input()
print S.replaceSpace(s)
except:
break
==有两点需要注意:
1、python3.6系列不支持raw_input。
2、ans = ans.replace(s[i], ‘%20’),为什么在这里必须要重新加一个变量才能replace,s = s.replace(s[i], ‘%20’)就不能replace。???????