牛客网-剑指offer刷题记录-2替换空格

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。

例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

思路:

用python自带的replace替换函数

 

代码如下:

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        s = s.replace(' ','%20')
        return s

s = Solution()
L = raw_input()
print(s.replaceSpace(L))

总结:

python3中的输入函数为input()

python2中的输入函数为raw_input()

猜你喜欢

转载自blog.csdn.net/scottzeg/article/details/81588910