Strings (Python3)

--Originally published at Elu's Blog

Strings are a basic type of data in Python3. These consist of chains of characters put together. Outside of the computer sciences, they are called ‘words’. But in a program a string could be just ‘lasdlk9(&(=lkasd3smds8098*^sdokads’.

If you have a variable x which has the value 390 in it (x = 390). To transform it to a string, it is very easy, just use the function str(). Like this: x = str(x). This will make it so x is ‘390’ instead of 390. The difference is that when 390 is a string you can’t do ‘x + 3’ and expect it to return a 393, it won’t work because the 3 and the ‘390’ are different types.

Sum and multiplication of strings

If you have a variable x = ‘egg’ and the variable y = ‘foo’, if you write ‘x + y’ the result will be ‘eggfoo’, the strings combine to make a new string.

If you have a variable x = ‘egg’ and write x *= 3, the result will be ‘eggeggegg’, it will add the same string three times.

Here is an alternative explanation:

Python 3 Tutorial: Print Function and Strings by sentdex