Operator khusus Python
Python-Specific Operators
For example, the code below uses concatenation to join the "Hello " and "World"
strings together to build "Hello World" and print it to the terminal:
my_str = "Hello " + "World"
print(my_str)
We can concatenate string variables together too, for example, this code
would print "OneTwo" to the terminal:
str_one = "One"
str_two = "Two"
result = str_one + str_two
print(result)
However, if we want to concatenate a string and an integer type together
using the + operator, we first need to coerce the integer into a string
using the str() method or the terminal will throw an error:
result_one = "John" + 42 // would throw a TypeError
result_two = "John" + str(42) // no error
For example, the code below inserts the num variable value into the
string so "This is number 33" will be printed to the terminal:
num = 33
my_string = f'This is number {num}'
print(my_string)
Note the use of the f before the string quotes, and the curly
braces that wrap around the variable name. Also, note that you do
not need to coerce the type of a variable before inserting it into a
string here, when you use f-strings to format your strings this is
done for you. Which makes using f-strings extremely handy!
Gorgeous Goldfinch