Let’s consider you want to convert a floating point number into an integer. There are two ways of doing that.
Method 1 – Truncating
You can truncate the floating point number to just the portion before the decimal point. You can do that with the int() function which truncates the number towards zero.
>>> fpn = 57.83
Let’s say you want to compare two string to see if they have the same value. You can do it using the comparison operator is equal to which is represented by == (two equal signs).
>>> s1 = 'Dan'
>>> s2 = 'Dumitrache'
>>> if s1 == s2:
... print('Strings are equal!')
... print('Strings are not equal!')
Strings are not equal!
Let’s say you may want to construct a big string from smaller strings in Python. The solution is simple and it is called concatenation. As in Math, you can use the addition operator, which is +, to put one or more strings together and return a larger string.
>>> s1 = 'My name '
>>> s2 = 'is Dan.'
>>> new_string = s1 + s2
My name is Dan.
Let’s say you want to add a string and a number together. For example you have the string “The number of monkeys = ” and the number 17. You would like to add them together and display “The number of monkeys = 17”.