Input Format
The first line contains the integer, .
The next  lines each contain a line of the text.
Constraints
Neither && nor || occur in the start or end of each line.
Output Format
Output the modified text.

hackerrank regex substitution problem solution in python programming

Problem solution in Python 3 programming language.

import re

for line in range(int(input())):
    string = ''
    string = re.sub(r'(?<= )&&(?= )','and',input())
    string = re.sub(r'(?<= )\|\|(?= )','or',string)
    print(string)

Second solution

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
N = int(input())
for i in range(N):
    print(re.sub(r'(?<= )(&&|\|\|)(?= )'
lambda x: 'and' if x.group() == '&&' else 'or'input()))

Problem solution in Python 2 programming language.

import re

for line in range(int(raw_input())):
    string = ''
    string = re.sub(r'(?<= )&&(?= )','and',raw_input())
    string = re.sub(r'(?<= )\|\|(?= )','or',string)
    print string