0

I have the input s of string. I want to print string s in which all the occurrences of WUB are replaced with a white space.

s = input()
    print(s.split("WUB"))

Input : WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB

but the output I am getting is like this : ['', 'WE', 'ARE', '', 'THE', 'CHAMPIONS', 'MY', 'FRIEND', '']

instead I need output in string format, like this : WE ARE THE CHAMPIONS MY FRIEND

3 Answers 3

3

You can join the strings in the list produced by split with a space:

print(" ".join(s.split("WUB")))

You can also just use replace instead of split + join:

print(s.replace("WUB", " "))
Sign up to request clarification or add additional context in comments.

2 Comments

which of the two method join or replace is faster and space efficient?
replace is better since it's built for this purpose, and it's also faster because it iterates only one time.
0

You can apply the input in the print statement like this

s = input()
    print(*s.split("WUB"))

Notice * before s.split("WUB") this gives the desired output.

WE ARE THE CHAMPIONS MY FRIEND

Comments

0

Just join all elements from your list. See it below:

print(" ".join("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB".split("WUB")).strip())

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.