String Concatenation
>>> print ‘red’ + ‘yellow’
Redyellow
>>> print ‘red’ * 3
Redredred
>>> print ‘red’ + 3
Traceback (most recent call last):
File “”, line 1, in
TypeError: cannot concatenate ‘str’ and ‘int’ objects
>>> print ‘red’ + str(3)
red3
String Formatting with the % Operator
x = ‘apples’
y = ‘lemons’
z = “In the basket are %s and %s” % (x,y)
In the basket are apples and lemons
String Formatting with the { } Operators
Fname = “John”
Lname = “Doe”
Age = “24”
print “{} {} is {} years old.“ format(fname, lname, age)
print “{0} {1} is {2} years old.” format(fname, lname, age)
Using the Join Method In Python
‘ ‘ .join([‘the’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’])
‘the quick brown fox jumps over the lazy dog’
music = [“Metallica”, “Rolling Stones”, “ACDC”, “Black Sabbath”, “Shinedown”]
print ‘ ’.join(music)