Cody Bable

Generation

generate functionFri, 19 May 2023

Split the string into pairs of two characters. If the string contains an odd number of characters, then the missing second character of the final pair should be replaced with an underscore ('_'). def split_pairs(text: str) -> Iterable[str]:

def split_pairs(a): if len(a) % 2 == 0: return [a[i:i + 2] for i in range(0, len(a), 2)] else: return [a[i:i + 2] for i in range(0, len(a) - 1, 2)] + [a[-1] + '_']

Questions about programming?Chat with your personal AI assistant