Offer to prove safety record of the brush replace the space problem

1. Title Description

Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

2. Replace violence

First create a null string, then for each character;
if the character is not a space, the character is added to the new end of the string;
if the character is a space, add '% 20' to the new end of the string.

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        new_s = ''
        for i in s:
            if i == ' ':
                i = '%20'
            new_s = new_s + i
        return new_s

3. python built-in functions

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        return s.replace(" ","%20")
Published 38 original articles · 98 won praise · views 360 000 +

Guess you like

Origin blog.csdn.net/xijuezhu8128/article/details/104676722