0

I'm working on a python project and I want to replace a specific word in a line in a file (text.txt) with another string. Like I have this line <string name="AppName">old string</string> And I want to replace the old string by a variable. That's why I don't want to use replace('old string','new string') I want to replace it depending on an index. Any help is highly appreciated. To be more clear I want to replace whatever is between the first '>' and the second '<' with the new string

9
  • 1
    I don't get why str.replace won't fit Commented May 26, 2022 at 9:57
  • Because this word will be changed every time by another string so I won't always have that word Google Commented May 26, 2022 at 9:58
  • Please update your question with some more examples which don't have that word Google. Commented May 26, 2022 at 10:00
  • 1
    Maybe what you're trying to do is replace whatever is between the first '>' and the second '<'. Is that it? Commented May 26, 2022 at 10:10
  • 1
    @dinarobert I have edited my original answer. Maybe that works for you Commented May 26, 2022 at 10:17

2 Answers 2

2

This is will replace whatever is between the first '>' and the second '<' with the given string:

s = '<string name="AppName">old string</string>'

def replace(s, new):
    if (i := s.find('>')) >= 0:
        if (j := s[i:].find('<')) >= 0:
            s = s[:i+1] + new + s[j+i:]
    return s

print(replace(s, 'Google'))

Output:

<string name="AppName">Google</string>

Note:

This would be more typically dealt with using RE

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. What if this line exist in another file "text.txt" how can I use this function to replace it
@dinarobert I suggest you ask another question. There's nothing in your original question that mentions any kind of file handling
2

Use regex replace

from re import sub

string = "<string name='AppName'>Google</string>"
# anything between the >< will be converted to new_var_string
replace_string = sub(r'>\w+<', f'>{new_var_string}<', string)

example 1:

from re import sub

string = "<string name='AppName'>Google</string>"
# anything between the >< will be converted to new_var_string
new_var_string = 'youtube'
replace_string = sub(r'>\w+<', f'>{new_var_string}<', string)
print(replace_string)

output:

<string name='AppName'>youtube</string>

example 2:

from re import sub

string = "<string name='AppName'>Goo1231gl125125e</string>"
# anything between the >< will be converted to new_var_string
new_var_string = 'youtube'
replace_string = sub(r'>\w+<', f'>{new_var_string}<', string)
print(replace_string)

output:

<string name='AppName'>youtube</string>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.