Python language basics 36: string concatenation using the join method

Introduction

In the previous post we discussed how to use the ‘in’ operator to determine whether a certain element is found in a collection. You can apply this operator – and its negation ‘not in’ – on all collection types including strings.

In this post we’ll look at a useful String function called ‘join’ to concatenate bits of strings into a single one.

String concatenation

Basic string concatenation is performed using the addition operator ‘+’:

combined = "Welcome" + " to" + " my blog " + "dear " + "readers"
print(combined)

‘combined’ will become “Welcome to my blog dear readers” as expected. Do you recall that strings are immutable in Python – and in fact all major programming languages? The problem with the above statement is that it creates multiple strings in succession as the ‘+’ operator is applied:

Welcome
to
Welcome to
my blog
Welcome to my blog

etc.

‘combined’ will at the end point at the string “Welcome to my blog dear readers” in memory. The other values will be kept in memory until the garbage collector cleans them up as unused resources without references.

A more efficient solution is the join function although its usage is a bit strange at first sight. It operates directly on a string which will become a string delimiter. Here’s an example:

combined = '|'.join(("This", "is", "a", "delimited", "string"))
print(combined)

combined will become:

This|is|a|delimited|string

Can you guess why we needed 2 pairs of parenthesis? The inner pair is the way to declare the tuple collection which we discussed in this post. The join method takes only one argument and it must be a collection. Hence we need to wrap the strings to be concatenated in a collection. A string list works equally well:

combined = '|'.join(["This", "is", "a", "delimited", "string"])
print(combined)

‘combined’ will be the same as above.

How can we build the same “Welcome to my blog dear readers” like above? What is the delimiter here? It’s of course the space character ‘ ‘:

combined = ' '.join(["Welcome", "to", "my", "blog", "dear", "readers"])
print(combined)

‘combined’ will evaluate to “Welcome to my blog dear readers” as it was intended.

Read the next part here.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: