- 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
3 Respostas
+ 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))
+ 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")
- 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