I'm new to python and I'm trying to do the "Band name generator" kata on codewars but I'm having trouble with the join function and I do not know why. It says "can only join an iterable" but I am trying to join a list which is an iterable so I do not understand what is the issue here and how to solve this. Thank you so much in advance!
import re
regex = r'^[a-z]$|^([a-z]).*\1$'
def split(name):
return [char for char in name]
def band_name_generator(name):
if (re.search(regex, name)):
x = split(name).append(split(name)[1:])
return "".join(x)
else:
return "The " + name.capitalize()
print(band_name_generator("alaska"))
First of all, the built-in function list()
does the same as your split function, so I would suggest using that if you can.
You can also splice strings as if they were lists and then join them together so the function band_name_generator()
could be rewritten as follows
def band_name_generator(name):
if (re.search(regex, name)):
x = name + name[1:]
return x
return "The " + name.capitalize()
The final else statement is not necessary as when a function returns it stops.
I would also suggest to use regex.compile
as recommended by the official docs.
import re
p = re.compile(r'^[a-z]$|^([a-z]).*\1$')
p.search(name)
In any case, the problem in your code is that append()
does not return a new list but modifies the original one, so your value x is empty (NoneType
)