Legacy - String Concatenation and Replication
- 02:00
Learn how to concatenate and replicate in Python.
Downloads
No associated resources to download.
Transcript
Let us now look at string concatenation and replication Python. String concatenation and replication simply means combining values and replicating values in Python, we use the plus sign to concatenate and the star sign to replicate values. However, there are some rules on what data types can be concatenated. And what data types can be replicated. Like we can see on the screen, we can concatenate two strings together, but we cannot concatenate a string with an integer. For example, adding the string names current under string named assets, would return a combined value of current assets without any spacing. Similarly, we can replicate a string with an integer. For example, multiplying the string named balance by five would return a balance string based at five times without any spacing. So we can only apply concatenation to the same type items. Similarly, we cannot replicate a string by multiplying it with a floating point number. This is because imagine, if we have a string named balance, and we try multiplying it by a floating point number 5.5, we cannot have a balanced based at five times and a half balanced based, it doesn't make sense. So replication only is possible when you multiply either a string by an integer or integer pioneer. Finally, one thing to note is that when a number is inputted into Python, it takes it as an integer by default. Similarly, if you input a floating point number to Python, it considers it as a float by default, but if you try to input a word, without defining that it is a string, it returns an error. So it is really important to input a word in quotes in Python.