8.3 Concatenating and Repeating Strings

  • Previously, we used the + operator to concatenate strings and the * operator to repeat strings
  • Also can perform these operations with augmented assignments
  • Strings are immutable, so each operation assigns a new string object to the variable
In [1]:
s1 = 'happy'
In [2]:
s2 = 'birthday'
In [3]:
s1 += ' ' + s2
In [4]:
s1
Out[4]:
'happy birthday'
In [5]:
symbol = '>'
In [6]:
symbol *= 5
In [7]:
symbol
Out[7]:
'>>>>>'

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.