简体   繁体   中英

Remove spaces and colon from a string

I'm trying to remove all the spaces and colon from a given string, using a single function, but I'm unable to achieve these two things in a single function can someone please help me on this?

def normalize_string1(string):
    return string.replace(" ", "")

def normalize_string2(string):
    return string.replace(":- ", "-")

normalize_string("AIX Server--1uryeu6438shdj:-thsoanfg_321-aq.com")

Write This code for your string. It will definitely work. This is a very common mistakes developer makes while working with strings. Here I am taking a string as an example. You can also do both tasks using a single function using this code.

    global string
    string = "Hello Everyone :- from YOURNAME"
def dtring(string):
    string = string.replace(" ", "")
    string = string.replace(":-", "-")
    print(string)

dtring(string=string)

I used print statement to show you the changes. One of the major mistakes was you just changed the value of the string but you had not updated those changes to your original string. You can update that value by using the "string =" statement. I hope this is helpful for you. You may try this program with any string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM