Live2D

剑指Offer-002:替换空格

一、前言

本系列文章为《剑指Offer》刷题笔记。

刷题平台:牛客网

二、题目

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

1、编程实现

#! /usr/bin/env python3
# -*- coding:utf-8 -*-

# Author   : Ma Yi
# Blog     : http://www.cnblogs.com/mayi0312/
# Date     : 2020-04-22
# Name     : test02
# Software : PyCharm
# Note     : 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经
#            过替换之后的字符串为We%20Are%20Happy。


class Solution:

    # s 源字符串
    @staticmethod
    def replace_space(s):
        # write code here
        return s.replace(" ", "%20")


def main():
    text = "We Are Happy"
    new_text = Solution.replace_space(text)
    print(new_text)

if __name__ == '__main__':
    main()

 

posted @ 2020-04-22 10:15  骑着螞蟻流浪  阅读(204)  评论(0编辑  收藏  举报