Two arithmetic operators work on strings with new meanings. + concatenates: 'Py' + 'thon' is 'Python'. Both operands must be strings; 'Age: ' + 30 raises TypeError, so convert first with str(30) or use an f-string. * replicates: 'ab' * 3 is 'ababab', and the order can be swapped, 3 * 'ab'. The other operand must be an integer. Multiplying by 0 or a negative number gives the empty string rather than an error. The augmented forms += and *= work too and, because strings are immutable, they create new strings and rebind the name.
Keep reading for free
Create a free StudyToCert account to read the rest of this lesson: 5 more sections, 3 key terms, a real-world example, an exam tip and self-check questions. Every lesson, lab and practice test is free with an account.