Skip to main content
Engineering LibreTexts

2.6: String operations

  • Page ID
  • In general, you can’t perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:

    'chinese'-'food'    'eggs'/'easy'    'third'*'a charm'

    But there are two exceptions, + and *.

    The + operator performs string concatenation, which means it joins the strings by linking them end-to-end. For example:

    >>> first = 'throat'
    >>> second = 'warbler'
    >>> first + second

    The * operator also works on strings; it performs repetition. For example, 'Spam'*3 is 'SpamSpamSpam'. If one of the values is a string, the other has to be an integer.

    This use of + and * makes sense by analogy with addition and multiplication. Just as 4*3 is equivalent to 4+4+4, we expect 'Spam'*3 to be the same as 'Spam'+'Spam'+'Spam', and it is. On the other hand, there is a significant way in which string concatenation and repetition are different from integer addition and multiplication. Can you think of a property that addition has that string concatenation does not?

    • Was this article helpful?