Arithmetic with strings
Similar to R, you can use Python as a calculator to perform arithmetic on numerical values.
For example, adding two values, 42 + 3.14
or multiplying values, 7 * 9
.
However, unlike R, Python allows you to use certain mathematical operators on non-numeric values.
When you use +
on strings, it will concatenate them together:
print('hello' + 'world')
'helloworld'
When you multiply a string by an integer, n
, the string will be repeated n
times:
print('hello' * 3)
'hellohellohello'
Finally, if you place two strings next to each other, they will also be concatenated:
print('hello' 'world')
'helloworld'
This exercise is part of the course
Python for R Users
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Add 'the quick' to 'brown fox'
print('the quick' ____)