In this Answer, we will look at multiple ways of adding space between words in a string using Python.
join()
: If we get the input as a list of words, we can add space between the words using the join()
method.
+
: We can add two strings using the +
operator.
f
: We can add space between words and get a new string using f
strings syntax.
print
: We can add spaces between words by listing them as commas separated in the print()
statement.
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 f
strings. So, we add space after each word in an f
string.
word1 = "Blue"word2 = "Green"#example using f stringsprint(f"{word1} {word2}")
A print
statement 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