Create a new string from an old string using python(1)


username

My function has three string parameters, string, search and replace. If the search argument is an empty string, the function should insert replaceable arguments before the first argument, between each character of the old string, and after the last character of the old string. Here's what I've done so far:

def main():
    s='I am going go'
    x=""
    y='aa'

    print(rep_str(s,x,y))

def rep_str(s, x, y):

    if x in s:
        result = ''
        i = 0
        while i < len(s):
            if s[i : i + len(x)] == x:
                result += y
                i += len(x)
            else:
                result += s[i]
                i += 1


    elif x not in s:
        result= s
    else:
        if x=="":
            result=y        
            for ch in s:
                result+=(ch+y)   

    return result
main()  

I developed each condition separately and put them together in a function when I got a satisfactory result. My last other condition is that it works fine in a separate run, but not in a function module. I don't know what's wrong with the code. I would appreciate if someone could give me some guidance. thanks

The output of the last else condition should be:

aaIaa aaaaamaa aagaaoaaiaanaagaa aagaaoaa
zhangxaochen

Restructure your code:

if x=='':
    pass
elif x in s:
    pass
elif x not in s:
    pass

Related


Create a new string from an old string using python(1)

username My function has three string parameters, string, search and replace. If the search argument is an empty string, the function should insert replaceable arguments before the first argument, between each character of the old string, and after the last ch

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

How to create new string from old string in Kotlin

Chris John I have created a new string invokeToken from the old string vitateCode using replace, but the value of invokeToken is not being passed to the startmeet function. (deepLink?.getQueryParameter("inviteCode")?.substringBefore("?inviteToken=","") + deepL

Create new string from other string (Java)

Lucas W. I want to extract data from a given string. example: I have something like: String s = "tree;dog;house;computer sience"; My desired result is: String a = "tree"; String b = "dog"; String c = "house"; String d = "computer sience"; Substrings are of v

Create variable from text string using new lines and quotes

Clark I have some content in a text file. I want to load it into a variable in PHP I just need to copy/paste the text directly into the variable without escaping all the quotes etc. How to do such a thing? I'm not looking for a solution file_get_contentbecause

Create new numeric column from 1 string column

Roger Oba I am a beginner. I got a dataset from here that contains profiles of people with different attributes, and it is. There are 12 majors: Administration, Blue Collar, Entrepreneur, Maid, Management, Retirement, Self Employed, Service, Student, Technicia

Create new numeric column from 1 string column

Roger Oba I am a beginner. I got a dataset from here that contains profiles of people with different attributes, and it is. There are 12 majors: Administration, Blue Collar, Entrepreneur, Maid, Management, Retirement, Self Employed, Service, Student, Technicia

Create new list from old list using re.sub() in python 2.7

go out My goal is to take an XML file, extract all instances of a specific element, remove the XML tags, and then process the rest of the text. I started with this and it can remove XML tags, but only from the entire XML file: from urllib import urlopen import

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create a new string in a function using a pointer

Alex Clynes I'm working on C pointers and how to pass them to functions, so I made this program #include <stdio.h> char* my_copy(pnt); void main() { int i; char a[30]; char* p,*pnt; printf("Please enter a string\n"); gets(a); pnt =

Create new line in string using jQuery

username I have a string that looks like this "Hello Name, Choose one of the options below.\n\n1. Phones\n\n2. Computers"When returning the output, I can't see the newlines taking effect. What might I be missing? JS var message = "Hello Name, Choose one of the

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create new line in string using jQuery

username I have a string that looks like this "Hello Name, Choose one of the options below.\n\n1. Phones\n\n2. Computers"When returning the output, I can't see the newlines taking effect. What might I be missing? JS var message = "Hello Name, Choose one of the

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create new line in string using jQuery

username I have a string that looks like this "Hello Name, Choose one of the options below.\n\n1. Phones\n\n2. Computers"When returning the output, I can't see the newlines taking effect. What might I be missing? JS var message = "Hello Name, Choose one of the