Swap a character with its next character in paragraph
Question: **I have to swap a specific character appearing in paragraph to its next
character.**

let suppose that my paragraph text is:

**My name is andrew. I am very addicted to python and attains very high
knowledge about programming.**

Now, my task is to find particular character in paragraph and swap it with the
character next to it. Like, I want to swap every character 'a' with its the
character next to it. After process my paragraph should look like this:

**My nmae is nadrew. I ma very dadicted to python nad tatians very high
knowledge baout progrmaming.**

I would be very thankful if anybody define function for this in python


Answer: This will do it:

    
    
    >>> import re
    >>>>regex = re.compile(r'(a)(\w)')
    >>>>text = 'My name is andrew. I am very addicted to python and attains very high knowledge about programming.'
    >>> regex.sub(lambda(m) : m.group(2) + m.group(1), text)
    'My nmae is nadrew. I ma very dadicted to python nad tatians very high knowledge baout progrmaming.'
    

Explanation:

    
    
    (a)(\w)
    

Matches a, and put it on group 1, then matches another word character put in
group 2. Lambda expression for replacement switch these two groups.

If you want to match everything but spaces use :

    
    
    (a)(\S)
    

