替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

输入:s = "We are happy." 输出:"We%20are%20happy

class solution(object):
    def replacespace(self,s):
        """
        :type s: str
        :rtype: str
        """
        s = list(s)
        for i in range(s):
            if s[i] == ' ':
                s[i] = '%20'
        return ''.join(s)

 

posted @ 2020-08-28 08:43  ninian  阅读(61)  评论(0)    收藏  举报