Home > OS >  I'm trying to run the rearrange_name function to rearrange the variable "name" but i
I'm trying to run the rearrange_name function to rearrange the variable "name" but i

Time:09-26

import re

def rearrange_name(name):
    result= re.search(r'^([\w.]*),([\w.]*)$', name)
    return '{} {}'.format(result[2], result[1])

print(result=rearrange_name('Lovely Ada'))

The NoneType error message

CodePudding user response:

The space separating the groups is not being captured by your regex pattern

import re

def rearrange_name(name):
    result= re.search(r'^(\w*)[ ,](\w*)$', name)
    return '{} {}'.format(result[2], result[1])

print(rearrange_name('Lovely Ada'))
  • Related