剑指Offer(Python)替换空格
推荐编程平台
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are
Happy.则经过替换之后的字符串为We%20Are%20Happy。
分析
题目要求替换空格,使用Python
可以轻松的实现这个效果,使用Python的内置函数replace
即可 replace()
str.replace(old, new[, max])
old
-- 将被替换的子字符串。new
-- 新字符串,用于替换old子字符串。max
-- 可选字符串, 替换不超过 max 次
代码
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(' ','%20')