Sword refers to the offer of space replacement (Python) __python

Source: Internet
Author: User
Topic DescriptionImplement a function that replaces a space in a string with "%20." For example, when the string is Are Happy, the replaced string is We%20are%20happy.
It would be easier to use Python, three ideas: 1, using Python's append function 2, using Python's split function 3, using strings to stitch code as follows:
#-*-Coding:utf-8-*-
"" "
Created on Sat Oct 17:20:46 2017
@author: Gb_xiao
mail:mingliumengshao@163 . com "" "" "
topic Description:
Please implement a function that replaces a space in a string with"%20 ".
For example, when the string is the We Are Happy, the character string after the replacement is
we%20are%20happy. "" "
def function1 (string): Result
    = [] for
    i in string:
        if i = =" ":
            result.append ("%20 ")
        Else:
            result.append (i) return
    ". Join (Result)

def function2 (string):
    lists = String.Split ("") Return to
    '%20 '. Join (lists)

def function3 (string): Result
    = "" For
    I in string:
        if i = = "": result
            = result + "%20"
        else: result = result + I return result

def main (): 
  #    string = "We Are Happy" Print
    function1 ("We Are Happy") Print
    function2 ("We Are Happy")
    print Fu Nction3 ("We Are Happy")
    
if __name__  = = "__main__":
    Main ()
    








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.