Join a list of strings into one string in python

Join a list of strings into one string in python

You can join a list of strings into one string in Python using the str.join() method. This method takes an iterable (like a list) of strings and concatenates them using the provided separator.

Here’s how you can use str.join():

Python
string_list = ["Hello", "world", "how", "are", "you?"]
separator = " "  # Space

result_string = separator.join(string_list)
print(result_string)  # Output: "Hello world how are you?"

In this example, the elements of the string_list are concatenated into a single string separated by spaces.

You can use different separators based on your requirements:

Python
string_list = ["apple", "banana", "orange"]
comma_separated = ", ".join(string_list)
semicolon_separated = ";".join(string_list)
newline_separated = "\n".join(string_list)

print(comma_separated)      # Output: "apple, banana, orange"
print(semicolon_separated)  # Output: "apple;banana;orange"
print(newline_separated)    # Output:
                            # "apple
                            # banana
                            # orange"

str.join() is a convenient way to concatenate strings from a list or any iterable while specifying the separator between them.

Total
0
Shares

Leave a Reply

Previous Post
Replace all occurrences of one substring with another substring in python

Replace all occurrences of one substring with another substring in python

Next Post
Case insensitive string comparisons in python

Case insensitive string comparisons in python

Related Posts