Search code examples
pythonsplat

How to print a list using splat-operator (*) without spaces


I'm trying to understand splat-operators in python. I have a code:

word = ['s', 't', 'a', 'c', 'k', 'o', 'v', 'e', 'r', 'f', 'l', 'o', 'w']
print(*word)

output:

s t a c k o v e r f l o w

I can't assign *word to some variable to check its type or smth else in debug. So I wounder which way print() gets the sequence of *word and if it's possible to print this word without spaces. Desirable output:

stackoverflow

Solution

  • You get that result because print automatically puts spaces between the passed arguments. You need to modify the sep parameter to prevent the separation spaces from being inserted:

    print(*word, sep="")  # No implicit separation space
    stackoverflow