How to add a space between words in a string in Python
In this Answer, we will look at multiple ways of adding space between words in a string using Python.
Approaches
join(): If we get the input as a list of words, we can add space between the words using thejoin()method.+: We can add two strings using the+operator.f: We can add space between words and get a new string usingfstrings syntax.print: We can add spaces between words by listing them as commas separated in theprint()statement.
Code examples
We add spaces between words using the
join()method, which accepts a list and returns a string.
list_of_words = ["Apple", "Banana", "Orange"]#example using join()print(" ".join(list_of_words))
We can use
+operator to concatenate two strings. So, we add space between words by concatenating them with an empty string containing a space.
word1 = "Blue"word2 = "Green"#example using + operatorprint(word1 + " " + word2)
We can dynamically provide values in strings using variables in
fstrings. So, we add space after each word in anfstring.
word1 = "Blue"word2 = "Green"#example using f stringsprint(f"{word1} {word2}")
A
printstatement adds a space after each word specified in the statement, separated by commas.
word1 = "Blue"word2 = "Green"#example using print statementprint(word1, word2)
Free Resources