 # Split pairs

I am stuck in this exercise :
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 (’_’).
here is my solution:

``````x = "codeacademy"
new_lst = []

for i in range(0, len(x), 2):
new_lst.append(x[i : i+2]
print(new_lst)
``````

This one work to split the string into pairs but I am stuck where I suppose to add “_” if the last character is single so I tried

``````if len(x) % 2 == 1:
x = x.append("_")

``````

but it didn’t work?

because `append()` modifies the list in place, the method itself doesn’t have a return value, so now you overwrite x with None (the absence of a return value)

furthermore, `.append()` a list method. Use string concatenation instead.

1 Like

Thanks that helps a lot:
here is my solution

``````# Split Pairs
# 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(a):
new_lst = []

if len(a) % 2 != 0:
a += "_"

for i in range(0, len(a), 2):
new_lst.append(a[i : i + 2])
return new_lst

print(split_pairs("abcd"))  # == ['ab', 'cd']
print(split_pairs("abc"))  # == ['ab', 'c_']
print(split_pairs("abcdf"))  # == ['ab', 'cd', 'f_']
print(split_pairs("a"))  # == ['a_']
print(split_pairs(""))  # == []
``````