[Python] JOIN
separator.join(iterable)
How did I encounter this?
When there are multiple release dates, I needed to add multiple lines dynamically at the end of the query string. Python's `join()` function is helpful in this case.
Syntax
separator.join(iterable)
words = ["apple", "banana", "cherry"]
result = ", ".join(words)
print(result)
# Output: "apple, banana, cherry"
Explanation
- First, a string of separator like `,` should be provided.
- From that separator, call `join()` with an `iterable`
How this is applied to my code
def build_insert_query(base_query: str, num_values: int) -> str:
values_string = ", ".join(["(%s, %s, %s, %s)"] * num_values)
return base_query + f"VALUES {values_string};"
Comments
Post a Comment