To make a type hint for a tuple with an undetermined number of strings in Python, you can use the
Union types from the
typing module. Here's an example:
from typing import Tuple, Union def process_strings(strings: Tuple[Union[str, ...], ...]) -> str: return ", ".join(strings) strings1 = ("hello", "world") strings2 = ("foo", "bar", "baz") print(process_strings(strings1)) # Output: "hello, world" print(process_strings(strings2)) # Output: "foo, bar, baz"
In the type hint above,
Tuple is used to indicate that we are working with a tuple, and
Union is used to indicate that each element of the tuple can be either a
str or an ellipsis (
...), which represents an infinite number of
str elements. The second
... indicates that the tuple can contain an undetermined number of elements.