题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

分析

1.直接replace方法替换

2.生成一个空字符串,遍历输入字符串,若是空格则往新字符串加要替换的内容,否则不变

解题

# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        s_new = s.replace(" ", "%20")
        return(s_new)

 

# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        s_new=""
        for ch in s:
            if(ch == " "):
                s_new += "%20"
            else:
                s_new += ch
        return(s_new)

 

相关文章:

  • 2021-11-24
  • 2021-06-22
  • 2021-07-23
  • 2021-07-03
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-09-22
  • 2022-01-18
  • 2021-07-18
  • 2022-01-12
  • 2021-10-13
  • 2021-12-29
  • 2021-07-28
相关资源
相似解决方案