Python String join() Method Examples

In the previous article, we have discussed Python Program for isdigit() Function
String join() Function in Python:

The join() method joins all items in an iterable into a single string.

As the separator, a string must be specified.

Syntax:

string.join(iterable)

Parameters:

iterable: This is required. Any iterable object whose returned values are all strings.

Examples:

Example1:

Input:

Given list = ['welcome', 'to', 'python', 'programs']
Given separator = '@'

Output:

The given list items after applying join() function :  welcome@to@python@programs

Example2:

Input:

Given tuple = ('hello', 'all', 'good', 'morning')
Given separator = '-'

Output:

The given tuple items after applying join() function :  hello-all-good-morning

Program for String join() in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the tuple as static input and store it in another variable.
  • Give the set as static input and store it in another variable.
  • Give the first separator as static input and store it in another variable.
  • Give the second separator as static input and store it in another variable.
  • Give the third separator as static input and store it in another variable.
  • Apply join() function to the given list to joins all items in a list into a single string for the given separator1.
  •  Store it in another variable.
  • Apply join() function to the given tuple to joins all items in a tuple into a single string for the given separator2.
  • Store it in another variable.
  • Print the result string after joining the given list items using the join() function.
  • Print the result string after joining the given tuple items using the join() function.
  • Similarly, do the same for the given set with the given third separator and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['welcome', 'to', 'python', 'programs']
# Give the tuple as static input and store it in another variable.
gvn_tupl = ('hello', 'all', 'good', 'morning')
# Give the set as static input and store it in another variable.
gvn_set = ('This', 'is', 'python', 'learning', 'platform')
# Give the first separator as static input and store it in another variable.
gvn_sepatr1 = '@'
# Give the second separator as static input and store it in another variable.
gvn_sepatr2 = '-'
# Give the third separator as static input and store it in another variable.
gvn_sepatr3 = '#'
# Apply join() function to the given list to joins all items in a list into a
# single string for the given separator1.
# Store it in another variable.
rsltstrng1 = gvn_sepatr1.join(gvn_lst)
# Apply join() function to the given tuple to joins all items in a tuple into a
# single string for the given separator2.
# Store it in another variable.
rsltstrng2 = gvn_sepatr2.join(gvn_tupl)
print("The given list items after applying join() function : ", rsltstrng1)
print("The given tuple items after applying join() function : ", rsltstrng2)
# Similarly, do the same for the given set with given third separator and
# print the result string
rsltstrng3 = gvn_sepatr3.join(gvn_set)
print("The given set items after applying join() function : ", rsltstrng3)

Output:

The given list items after applying join() function :  welcome@to@python@programs
The given tuple items after applying join() function :  hello-all-good-morning
The given set items after applying join() function :  This#is#python#learning#platform

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(), input(), and split() functions.
  • Store it in a variable.
  • Give the tuple as user input using list(), input(), and split() functions.
  • Store it in another variable.
  • Give the set as user input using list(), input(), and split() functions.
  • Store it in another variable.
  • Give the first separator as user input using the input() function and store it in another variable.
  • Give the second separator as user input using the input() function and store it in another variable.
  • Give the third separator as user input using the input() function and store it in another variable.
  • Apply join() function to the given list to joins all items in a list into a single string for the given separator1.
  •  Store it in another variable.
  • Apply join() function to the given tuple to joins all items in a tuple into a single string for the given separator2.
  • Store it in another variable.
  • Print the result string after joining the given list items using the join() function.
  • Print the result string after joining the given tuple items using the join() function.
  • Similarly, do the same for the given set with the given third separator and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),input() and split() functions.
# Store it in a variable.
gvn_lst = list(input(
    'Enter some random List Elements separated by spaces = ').split())
# Give the tuple as user input using list(), input(),and split() functions.
# Store it in another variable.
gvn_tupl = tuple(input(
    'Enter some random Tuple Elements separated by spaces = ').split())
# Give the set as user input using list(), input(),and split() functions.
# Store it in another variable.
gvn_set = set(input(
    'Enter some random Set Elements separated by spaces = ').split())
# Give the first separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr1 = input("Enter a random element = ")
# Give the second separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr2 = input("Enter a random element = ")
# Give the third separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr3 = input("Enter a random element = ")
# Apply join() function to the given list to joins all items in a list into a
# single string for the given separator1.
# Store it in another variable.
rsltstrng1 = gvn_sepatr1.join(gvn_lst)
# Apply join() function to the given tuple to joins all items in a tuple into a
# single string for the given separator2.
# Store it in another variable.
rsltstrng2 = gvn_sepatr2.join(gvn_tupl)
# Print the result string after joining the given list items using the join()
# function
print("The given list items after applying join() function : ", rsltstrng1)
# Print the result string after joining the given tuple items using the join()
# function
print("The given tuple items after applying join() function : ", rsltstrng2)
# Similarly, do the same for the given set with given third separator and
# print the result string
rsltstrng3 = gvn_sepatr3.join(gvn_set)
print("The given set items after applying join() function : ", rsltstrng3)

Output:

Enter some random List Elements separated by spaces = hello this is python learning platform
Enter some random Tuple Elements separated by spaces = p r q s t
Enter some random Set Elements separated by spaces = good morning all
Enter a random element = &
Enter a random element = *
Enter a random element = !
The given list items after applying join() function : hello&this&is&python&learning&platform
The given tuple items after applying join() function : p*r*q*s*t
The given set items after applying join() function : good!morning!all

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.