As you remember from the previous session Variables, we learned that variables are meant to store information to be retrieved at a later time.

Today we are going to have some fun manipulating variables.
Lets start with concatenation. In python concatenation is simply attaching, combining or binding one or more string to another. We do so by using the + sign, as mentioned in Basic Calculations Using Python

Remember, python would only do what you code it up to do.

Let’s say we have two variables whose values are, Hello and World,



if we want python to combine these two variables to form a sentence, we would ask python to do so within a print statement, separating both variables with a +,

Notice that it printed both variables as one word, because that’s the command we gave python.

If we want these two variables to form a sentence with a space in between both words, then we have to also add a space, by inserting a space in an empty string ” “, as shown below;

The same principle applies, regardless of the number of variables we are working with.

We could also reassign these two existing variables to a new variable and separate the two with a + sign, as shown in the images below.

So now when we print the new variable print(var), the value would be that of the first two variables (varone and vartwo), because the values of varone and vartwo were reassigned to vars.

Remember in order for this to be a sentence instead of one word, unless it is intended, then we have to add a space in an empty string to separate each word, as shown below.

Now when we print this, we would have two separate words in the sentence, as shown below.

Reassignment could also be used to edit existing variables, by reassignment new values to the same variable name. Lets say for instance I left out a letter or misspelled a word, I could fix that by reassigning the existing variable to the same variable.

We can also concatenate a print statement with existing variables, as shown in the image below.

As you can see we combined the print statement (“I love to say”), with our existing variable (var) in order to form this sentence.

The variable and print statement can be inserted in whatever position you desire, just like the example below.

There is a problem, the code is too long, we can do better by eliminating some of these empty strings that are meant to put a space between each word, by simply leaving a space before or after the words in strings, based on where the space is needed, that way we don’t have to concatenate an empty string between the variable and the strings. A great way to do this, would be as shown in the image below.


There is no better way to master your craft, than to practice.
We would continue manipulating variables in other blog post to come.


I hope this post was helpful.

Share your techniques and thoughts, by leaving your comments, in the comment section below.

Data Mrs Python, Tutorials

Leave a Reply

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