Explain join() and split() method with examples.

1. Explain join() and split() method with examples.

Answer:

join() Method

Used to combine a list of strings into one single string.
Syntax: 'separator'.join(list)

Example:

words = ['Hello', 'World']
print(' '.join(words))  # Output: Hello World

Here, ' ' (a space) is used as the separator between the words.


split() Method

Used to split a string into a list of words or parts, based on a separator.
Default separator: Space ' '

Example:

message = 'Python is fun'
print(message.split())  # Output: ['Python', 'is', 'fun']

Here, the string is split wherever a space is found.

Leave a Reply

Your email address will not be published. Required fields are marked *