Python provides a number of handy operations to manipulate strings. The code snippet below demonstrates some of these operations in action.
x = "one" y = "two" # Uppercase all characters in a string print(x.upper()) # Output: "ONE" # Capitalise the first character of a string print(x.capitalize()) # Output: "One" # Check if a string starts with another substring print(x.startswith("on")) # Output: True # Check if a string ends with another substring print(y.endswith("wo")) # Output: True # Check if a given substring is included in a string print("on" in x) # Output: True
We can also use the +
operator to concatenate two strings and the *
operator two repeat a string.
# + concatenates strings print(x + y) # Output: "onetwo" # * repeats a string a given number of times print(x * 3) # Output: "oneoneone"
Define the following variables:
level = "info" msg = "process completed successfully"
Using string operations on the variables above, produce the following string and assign it to a variable called log_record
:
"INFO: Process completed successfully."