A
A
Azamgl2020-05-27 10:21:55
Python
Azamgl, 2020-05-27 10:21:55

Python replace related question?

I need to write some code to change the text [b] and [/b] into <.b> and <./b> directly.
I used the replace function, but only one pair of these tags can be changed there.
That is, I wrote like this:
stroka = input()
new_stroka = stroka.replace("[b]", "<.b>", '[/b]', '<./b>')
print(new_stroka) , but this is a mistake.
stroka = input()
new_stroka = stroka.replace("[b]", "<.b>")
new_stroka = stroka.replace("[/b]", "<./b>")
print(new_stroka) , but in this case, only the second line changes, it ignores the first one,
Who knows how this can be implemented, help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-05-27
@Azamgl

stroka = '[b]ew f  f sd fa sd fasdfasdf[/b]'
stroka.replace("[b]", "<.b>").replace("[/b]", "<./b>")

'<.b>ew f f sd fa sd fasdfasdf<./b>'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question