Table of Contents
What is string concatenation in Python and how to do it
b. You can do this by placing the two strings next to each other, with the string on the left having a space before it.

Examples of string concatenation in Python
a. You can append a string to another by placing the string next to it with a space before it. You can also do it by writing the string on the left next to another string.
b. You can also append a string to a variable by using the “=” sign before the variable, and then placing the two strings next to one another with no spaces in between them.
#! /usr/bin/env python
print(“Hello, world”)
“Hello, world”
“This is an example of concatenation that appends.
This is an example of a concatenation that is not appending.
This is an example of concatenation that appends to a variable.”
#! /usr/bin/env python
print(“Hello, world”)
x = “Hello” print(x + ” world”)
#!/usr/bin/env python2.5
print(“Hello, world”)
def append1(x):
def append2(y):
x = “Hello” y = “world” print(append1(x) + append2(y))
#!/usr/bin/env python2.6

The + operator versus the % operator for string concatenation in Python
c. You can combine two strings with the + operator. This can be done in two different ways: placing the second string next to the first with or without a space before it. The % operator can only be used to create a new string that is a combination of two existing strings (concatenation).
d. You can combine two string with the % operator by giving the variable a name and then using the variable instead of its value in the concatenation expression. For example, “appending to a variable” would be appending to a new variable named: name
[[SIDE NOTE] this is not as easy as it seems; it needs an annotation about the difference between [ and ] ] .
e. You can’t do this with the + operator. The plus is usually used for:
i. appending to a list
ii. adding one value to another value
iii. removing an element from a list
iv. turning all but the last value into uppercase letters without removing the original list/string.
v. replacing a value in a list/string with another value.
f. You can combine two lists or two strings with the + operator but you cannot use the same operator to create both a list and an individual string from it.
g. You can’t do this with the % operator. The % is usually used for:
i. extracting one part of a whole name, e.g., “the first name”
ii. extracting one part of an address, e.g., “the street address”
How to append strings together in Python using the .join() function
d. You can join a list of strings together in Python using the .join() function. The join() function is used to combine a series of lists into a single list. This new list is then returned. If used without parentheses, the second argument must be a string. If a third argument is given, it can be either a string or a list of strings, if the arguments are of different types. You can also use the join() function to join a list of strings together.
For example, the code below joins two character strings together.
mylist = [‘one’,’two’,’three’]
mystring = ‘one’
mystring2 = ‘two’
print mylist.join(mystring,’-‘) # prints one-two-three
The code above is simple and short. It uses the join() function to append a known string (the character string “one”) to the list of strings to create “one-two-three”.

Other ways to append strings together in Python
b. [using placeholders] You can append a string to another by placing the string next to it with a space before it. You can also do it by writing the string on the left next to another string. This would make the string with a “b” as it’s first character.
c. The [list comprehension] method is available in Python 3.6 and later, but you won’t see it until you import the list module for Python 3.4:
import list
list . append ( ‘a’ )
[‘a’]
list . append ( ‘b’ )
[‘a’, ‘b’]
list . append ( ‘c’ )
check_append ([ 1 , 2 , 3 ], 9 ) #=> [1, 2, 3, 9]
function append$(sequence $s1 as sequence, … ) as sequence
sequence $return = []
foreach $s in $s1
$return << $s
for i in range ( 1 , len ( $s ) + 1 ) # More efficient way of doing the “lazy” loop. Doesn’t use all of the memory.
return $return
