The Noobs of Python: Ep.2.3 - String Basics

String Concatenation and Formatting

Going further in depth with Strings we touch on Concatenations. Concatenation is a big word that means to combine or add two things together. In this case, we want to know how to add two or more strings together. As you might suspect, this operation is very easy in Python as seen in the example below:

>>> L1 = 'Level1Techs'
>>> L1F = 'Forum'
>>> Concat = L1 + L1F
>>> print(Concat)
Level1TechsForum
>>>

Q: That was a very easy example of putting 2 variables together, What else can we do?
A: As you can see from the example above on Operators the + is the addition operator when it operates on two integers or floating-point values if you need to use a variable within a string like so:

>>> 'Level' + '1' + 'Techs'
'Level1Techs'
>>>

Note: If you try to use the + operator on a string and an integer value, Python will not know how to handle this, and it will display an error message as seen below.

    >>>
    >>> 'Level' + 1 + 'Techs'
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        'Level' + 1 + 'Techs'
    TypeError: Can't convert 'int' object to str implicitly
    >>>

Q: Ah cool ! You showed an error message ! I thought you never made mistakes Lol :slight_smile:
A: More on Converting Data Types and Error handling in a later Ep:

Q: Are there other ways to do string concatenation?
A: Absolutely ! Here is another example with a for loop

>>> for name in names:
	print('Hello ' + name)

	
Hello Wendell
Hello Twindell
Hello Gwendell
>>>

More in Loops in The Noobs of Python: Ep.2.1 - List : Slice, Loops and more [Updated]

I've shown you the + operator for some simple concatenation, but the truth is that using the + operator to join strings together is very inefficient and can potentially slow your program’s execution down. Python isn’t that slow. Often, it works out better to manipulate a list of words and then use string.join(sequence) to return a string that is a concatenation of the strings in the sequence.

>>> 
>>> l1 = 'Wendell'
>>> l2 = 'Twindell'
>>> l3 = 'Gwendell'
>>> 
>>> Level2 = ' '
>>> 
>>> Level2.join([l1, l2, l3])
'Wendell Twindell Gwendell'
>>>

Keep in mind that string.join() is expecting a sequence of strings as an argument.

>>> 
>>> l1 = 'Wendell'
>>> l2 = 'Twindell'
>>> l3 = 'Gwendell'
>>> Level2 = ' - '
>>> Level2.join([l1, l2, l3])
'Wendell - Twindell - Gwendell'
>>>

You may need to convert other data types into strings and join up any sublists first, so that you present the outermost join() with a list of strings.

>>> 
>>> ex = 'the Level1Techs forums'
>>> ex2 = 'member'
>>> l1 = ' '
>>> l1.join(('Wendell', l1.join([ex, ex2])))
'Wendell the Level1Techs forums member'
>>>

This is where you need to start keeping an eye out for nested parentheses. Idle3.5 comes in handy here for NoobsOfPython because it highlights what the last 3 parenthesis locks in

>>> l1.join(('Wendell', l1.join([ex, ex2])))

Try this out if you have Idle3.5. It does become confusing at time encapsulating all of these strings and methods together. Post your comments if you have any !

Here is a cleaner way of achiving the same results:

>>> Ex1 = l1.join([ex, ex2])
>>> l1.join(['Wendell', Ex1])
'Wendell the Level1Techs forums member'
>>>

Q: If the plus Operator can be used on the string for Concatenation what about the * Operator?
A: The * Operator is used for replication here is a quick example:

>>> 
>>> ex * 2
'the Level1Techs forumsthe Level1Techs forums'
>>>

Post some of your examples and questions below !

Code_On_Code_Warriors