Python Programming Tutorial for Beginners – Part 5 – String concatenation

String Concatenation

String Concatenation is adding, joining or putting 2 or more strings together. In python, we do this with the plus sign or addition operator. Let’s experiment a bit – but first let us create a new file called geezam_python_strings.py and inside that file lets type what I have copied on screen here or just follow me as I go

a = “Geezam”
b = “python”
c = “for”
d = “beginners”
e = ” ”
words = a + b+ c + d
print(words)

From the result, we can see we joined the string variables a,b,c and d together into the variable d. The output doesn’t look so good so let’s add some spaces using the e variable which is literally just a string that is a space.

niceWords = a + e + b + e + c + e + d
print(niceWords)

The strings that we concatenate don’t even need to be within a variable so let’s add a colon to make it even nicer

niceWords = a + “:”+ e + b + e + c + e + d
print(niceWords)

len() function

The may be times when you want to know the length of a string variable. You can find out this integer value by using python’s built-in length function. Let’s try it continuing in our open string file

f = len(a)
print(f)

The word “geezam” has 6 characters – G E E Z A and M so the integer value 6 was assigned to the variable f. What if we wanted to do something calculations with an integer value we get from the length function. Lets experiment

g = len(a) + len(b)
print(g)

Again the length of a is 6, the length of b is also 6. we add those values together and assign them to the variable g and we get = 12!

There are much more things we could do with strings but let us continue to keep it simple and build on what we have learned before so lets recap

recap

String concatenation is putting 2 or more strings together. These strings can be assigned to variables or not. the length function, when applied to a string, will return an integer value which is the number of characters that make up the string. don’t forget that spaces count!

Python Programming Tutorial for Beginners

The following two tabs change content below.
Kelroy is an internet junkie, technology advocate and avid gamer who envisioned Geezam.com as a Caribbean based technology blog offering a more global view of technology topics. Reach out if you have a guest post or would like to write for us.