Python Join/Concatenate String

Python Join/Concatenate String

Python string join; In this tutorial, you will learn how to join two or more (multiple string) string in Python with space by using space, comma and any delimiter.

The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string. The syntax of the join() method is: string.join(iterable)

To join the string, you can use the python join() function of Python. Here you will first learn about the python join function of join python such as the syntax of the join function, parameters, return value, etc. After this you will learn how to join a string in Python.

How to join string in python

The Python built-in string join() function is the concatenation of the strings in iterable with string object as a delimiter.

Note:- The python join() function is built-in python function.

Syntax of Python String join() function is:

str_object.join(iterable)

The iterable elements must be a string, otherwise TypeError will be raised.

The separator between elements is the string providing this method.

Parameters of join() function

The join() method takes an iterable – objects capable of returning its members one at a time

Some of the example of iterables are:

  • Native datatypes – List, Tuple, String, Dictionary and Set
  • File objects and objects you define with an __iter__() or __getitem()__ method

Return Value from join()

The join() method returns a string concatenated with the elements of an iterable.

Example 1: Python string join with comma

Let’s look at some examples of string join() method.

s1 = ','.join(['a', 'b', 'c'])

print(s1)

Output

a,b,c

The string object can have multiple characters too.

s = 'abc'

s1 = s.join('xyz')

print(s1)

Output: 

xabcyabcz

Notice that string is iterable. Here iterable elements are ‘x’, ‘y’ and ‘z’. They are concatenated together with ‘abc’ as the separator i.e. ‘x’+’abc’+’y’+’abc’+’z’.

Example 2: Python string join with space

We can concatenate a sequence of strings (list, tuple) using join() function. The string object should be an empty string.

s1 = ' '.join(['a', 'b', 'c'])
print(s1)

s1 = ' '.join(('a', 'b', 'c'))
print(s1)

Output:

abc
abc

Recommended Python Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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