Python String join() Method
The join() method is a string method that takes an iterable object (such as a list, tuple, string, set, or dictionary) and returns a string that is the concatenation of the elements of the iterable, separated by the string on which it is called.
Here are some examples of using the join() method:
# example 1: join a list of strings with a comma
sep = ','
names = ['Steve', 'Bill', 'Ravi', 'Jonathan']
print(sep.join(names)) # Steve,Bill,Ravi,Jonathan
# example 2: join a string with a comma
mystr = 'Hello'
print(sep.join(mystr)) # H,e,l,l,o
# example 3: join a tuple of strings with a comma
nums = ('1', '2', '3', '4')
print(sep.join(nums)) # 1,2,3,4
# example 4: join a set of strings with a comma
langs = {'Python', 'C#', 'Java', 'C++'}
print(sep.join(langs)) # Python,C#,Java,C++
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment