String reverse in Python
Question: Write a simple program that reads a line from the keyboard and outputs the
same line where every word is reversed. A word is defined as a continuous
sequence of alphanumeric characters or hyphen (‘-’). For instance, if the
input is “Can you help me!” the output should be “naC uoy pleh em!”

I just tryed with the following code, but there are some problem with it,

    
    
    print"Enter the string:"
    str1=raw_input()
    print (' '.join((str1[::-1]).split(' ')[::-2]))
    

It prints "naC uoy pleh !em", just look the exclamation(!), it is the problem
here. Anybody can help me???


Answer: The easiest is probably to use the `re` module to split the string:

    
    
    import re
    pattern = re.compile('(\W)')
    string = raw_input('Enter the string: ')
    print ''.join(x[::-1] for x in pattern.split(string))
    

When run, you get:

    
    
    Enter the string: Can you help me!
    naC uoy pleh em!
    

