Better approach to adding spaces in between words? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Better approach to adding spaces in between words?

I would like to know if there are better ways to adding spaces in between words, numbers, whatever it might be that I have to output for code coach challenges, since it seems that similar tasks await me https://code.sololearn.com/c5VXP7UmOwI8/?ref=app

28th Jul 2022, 5:48 AM
YuGiMob
YuGiMob - avatar
3 Answers
+ 1
you may use .join() method. Example: words = "sololearn is good".split() " ".join(words) Also you can use map() function to apply function to every item in array. Example: words = "sololearn is good".split() result = list(map(lambda x: x[::-1], words))
28th Jul 2022, 6:43 AM
Nikita M
Nikita M - avatar
+ 1
YuGiMob yet another way: text = input().split() for i in range(len(text)): text[i] = (text[i][1:] + text[i][0] + "ay") print(*text) The unpacking operator (*) pulls out individual elements to print, and print inserts a space between elements. While space is default, you can customize it with sep=<string>. Examples: print(*text, sep=", ") print(*text, sep="\n")
28th Jul 2022, 11:55 PM
Brian
Brian - avatar
- 1
Thanks, I actually forgot these approaches are possible. I will be going forward with an approach looking like the following: https://code.sololearn.com/cycQcShF97bF/?ref=app
28th Jul 2022, 7:09 AM
YuGiMob
YuGiMob - avatar