简体   繁体   中英

Need output in string type

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

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", " "))

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

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

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

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