August 17, 2024
Python join() function
join() function is one of the most important Python's built in function of an iterable. Sometimes it happened that you want to convert an iterable's elements into a string so for the same you can use join function in Python. implode function requires array/iterable as argument to join into an string. It is called on an separator string. The join function convert each element of an array/iterable into a string. join() function attach separator string after each element of the array. The example of join function is mentioned below:
Code
weekDaysArray = ("Sunday", "Monday", "Tuesday", "Wednesday", \
"Thursday", "Friday", "Saturday")
weekDaysString = ",".join(weekDaysArray)
print(weekDaysString);
In above mentioned example we have a tuple with the name of weekDaysArray which have all the week days. In line number 2 we have used join function with weekDaysArray tuple and provide separator string as comma(,) then join function returns a string of all the week days and we store it in weekDaysString variable and print it on the screen through print function.