I don't understand how this syntax work and can't find documentation on it:
word = '123xyz567'
s = ''.join (c if c.isdigit() else ' 'for c in word)
Line 2 of code will take xyz123 and join together "123 567"
into one string by passing in a lambda function to join
However, I am confused as to how this syntax works, normally a python if-else statement goes:
def example():
for c in word:
if c.isdigit():
return c
else:
return ' '
Can someone either explain or direct me to documentation that explains to me the syntax structure of the lambda function passed into .join()
and how I can use it properly?