Let’s start with Java language. Strings in Java, by default, are immutable – objects whose state cannot be modified after it is defined. The opposite of an immutable object is mutable.
Java programming languages natively support the StringBuilder class to support the mutability of Java strings.
Python Strings are also immutable. However, Python does not support StringBuilder natively, but we can use some equivalent methods to make a custom string builder.
This article will discuss the following methods:
- Method 1: Using <str>.join(iterable),
- Method 2: String concatenation using “+” operator, and,
- Method 3: Using StringIO() class in io module
Method 1: Using <str>.join(iterable)
The <str>.join(iterable) is used to join elements of the iterable on <str>. Here is an example.
1 2 3 4 5 6 |
lst1 = ["Apples", "Bananas", "Oranges", "Grapes"] # Joining all the elements of lst1 on whitespace. lst1_joined = " ".join(lst1) print(lst1_joined) |
Output:
Apples Bananas Oranges Grapes
Note that the elements of the iterable being joined must be of string type; otherwise TypeError exception like this will be raised.
TypeError: sequence item <index>: expected str instance, <non_int_type> found
Therefore, if you have an iterable with mixed datatype, you need to change them to strings. For example,
1 2 3 4 5 6 |
lst2 = ["Apples", 55, None, "Grapes"] # Using the map() function to convert each item in lst2 into a string and join the result on "#" lst2_joined = "#".join(map(str, lst2)) print(lst2_joined) |
Output:
Apples#55#None#Grapes
In the above example, map(<func>, iterable) applies the function, <func>, to each item in the iterable.
Method 2: Concatenating Multiple Strings
You can also build a custom string on Python using the “+” operator to concatenate multiple strings into 1.
If you have a few strings to merge, you can use something like this.
1 2 3 4 5 6 7 |
str1 = "This" str2 = "is" str3 = "awesome" # Concaternative the three strings as str4. str4 = str1 + str2 + str3 print(str4) |
Output:
Thisisawesome
However, a for-loop can suffice if you have a long list of strings you want to join.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
lst1 = ["Apples", "Bananas", "Oranges", "Grapes", "Berries"] str2 = "" # Initialize an empty string. for item in lst1: # Concatenate str2 with the item and add # white space at the end of the item str2 += item + " " # The line above is the same as # str2 = str2 + item + " " # Note: if any item in lst1 is not a string, you need # to convert into a string first before concatenating. # You can do that with str() as follows: # str2 += str(item) + " " print(str2) |
Output:
Apples Bananas Oranges Grapes Berries
Method 3: Using StringIO() Class on io Module
The following example shows how to use this class to join strings. The code contains comments to help you understand the lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from io import StringIO lst1 = ["This", "is", "awesome"] # Create StringIO instance str1 = StringIO() for item in lst1: # Loop through each item in the list and write # item + space into str1 str1.write(item + " ") # If one of the items in the lst1 is not a string, you need # to convert it to string as str(item) # Get the value of str1 print(str1.getvalue()) |
Output:
This is awesome
Conclusion
Python does not natively support StringBuilder; however, you can create custom builders using the three methods discussed in this article. The first method is a method commonly used to join strings in Python.