Python language basics 37: splitting a delimited string

Introduction

In the previous post we saw how to efficiently concatenate strings using the join function. We showed a little trick to build one string from several others with a space in between.

In this post we’ll quickly look at the exact opposite, i.e. how to split a delimited string.

Read more of this post

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.

Python language basics 35: determine if an element is included in a collection

Introduction

In the previous post we looked at how to return multiple values from a function in Python. We saw that this is fact was only possible through a trick where the trick was to use a collection of type Tuple. We went through an example with a simple function that returned 4 values in a tuple. We also discussed how to access individual elements in the tuple.

In this post we’ll see how to determine if a certain element is located in a collection.

Read more of this post

Python language basics 34: returning multiple values from a function

Introduction

In the previous post we looked at the idea of local and global variables. We saw how a variable with function-level focus could shadow another variable with the same name but global scope. Finally we discussed how to indicate our intention to use the global level variable using the “global” keyword.

In this post we’ll look at a technique to return multiple values from the same function.

Read more of this post

Python language basics 33: variable shadowing and the ‘global’ keyword

Introduction

In the previous post we looked at positional and keyword arguments in a function. We saw how positional arguments were matched up with the arguments in the function signature. We also discussed how keyword arguments could make your code cleaner by explicitly providing the argument names in a function call.

In this post we’ll look at how a variable declared within a function can overshadow another variable declared outside of it.

Read more of this post

Python language basics 32: positional and keyword arguments in a function

Introduction

In the previous post we discussed how to provide optional arguments to a function. We saw how easy it was to assign a default value to a function argument using the assignment operator ‘=’. The caller could optionally ignore those arguments so that the default ones would be used within the function body. Alternatively the caller could override the defaults and provide its own argument values.

In this post we’ll look at another feature related to function arguments: positional and keyword arguments.

Read more of this post

Python language basics 31: mutable objects as default arguments

Introduction

In the previous post we discussed how to provide optional arguments to a function. We saw how easy it was to assign a default value to a function argument using the assignment operator ‘=’. The caller could optionally ignore those arguments so that the default ones would be used within the function body. Alternatively the caller could override the defaults and provide its own argument values.

In this post we’ll discuss an additional facet of optional function arguments. Optional arguments are evaluated only once along with the function. This can cause unexpected results when mutable objects are passed as default arguments.

Read more of this post

Python language basics 30: providing default values to a function

Introduction

In the previous post we looked at how objects are passed by reference to function in Python. We saw how it could be dangerous to let a function modify an incoming parameter if you’re not aware of the reference passing behaviour. We also briefly looked at the idea of a deep copy which is one way to keep your original objects intact.

In this post we’ll diverge from objects and instead investigate a feature of functions: default arguments.

Read more of this post

Python language basics 29: passing the reference value into a function

Introduction

In the previous post we looked at the effects of modifying the value of an object. We saw the difference between mutable and immutable objects. Immutable objects cannot be changed directly in memory whereas mutable objects can. You need to keep in mind that if two object variables reference the same value then changing one variable value will also change the value of all other references.

In this post we’ll discuss what this behaviour means when objects are passed as arguments into functions. Functions will be able to modify the referenced argument which will also affect the object that was originally passed in. If you’re not aware of this behaviour it can cause irritation and buggy code.

Read more of this post

Python language basics 28: modifying the referenced value

Introduction

In the previous post we discussed the basics of value and reference equality. We saw how to evaluate these equality types with the ‘==’ and ‘is’ operators. We also found an important difference between how value and reference types are handled in memory.

In this post we’ll look at another feature related to objects and equality, namely the effects of changing the referenced value.

Read more of this post

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

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