The re.sub() tool (sub stands for substitution) evaluates a pattern and, for each valid match, it calls a method (or lambda). 

The method is called for all matches and can be used to modify strings in different ways. 

The re.sub() method returns the modified string as an output.

Learn more about $re.sub()$.

Transformation of Strings  

Code  

import re

#Squaring numbers
def square(match):
    number = int(match.group(0))
    return str(number**2)

print re.sub(r"\d+", square, "1 2 3 4 5 6 7 8 9")

Output  

1 4 9 16 25 36 49 64 81

Replacements in Strings

_{Code}  

import re

html = """
<head>
<title>HTML</title>
</head>
<object type="application/x-flash" 
  data="your-file.swf" 
  width="0" height="0">
  <!-- <param name="movie"  value="your-file.swf" /> -->
  <param name="quality" value="high"/>
</object>
"""

print re.sub("(<!--.*?-->)", "", html) #remove comment

Output

<head>
<title>HTML</title>
</head>
<object type="application/x-flash" 
  data="your-file.swf" 
  width="0" height="0">

  <param name="quality" value="high"/>
</object>

Task

You are given a text of $N$ lines. The text contains && and || symbols. 

Your task is to modify those symbols to the following:  

&& → and
|| → or

Both && and || should have a space " " on both sides. 

Input Format

The first line contains the integer, $N$. 

 The next $N$ lines each contain a line of the text.

Constraints

$0<N<100$  

Neither && nor || occur in the start or end of each line. 

Output Format

Output the modified text.

Sample Input
11
a = 1;
b = input();

if a + b > 0 && a - b < 0:
    start()
elif a*b > 10 || a/b < 1:
    stop()
print set(list(a)) | set(list(b)) 
#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.

Sample Output
a = 1;
b = input();

if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
print set(list(a)) | set(list(b)) 
#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.

Difficulty: MEDIUM

Solutions:
import re

def change(match):
	symb = match.group(0)
	if symb == '&&':
		return 'and'
	elif symb == '||':
		return 'or'
n = int(input().strip())
for _ in range(n):
	print(re.sub('(?<= )(&&|\\|\\|)(?= )', change, input()))


import re
n = int(input())
print('\n'.join(list((re.sub('(?<=\\s)\\|\\|\\s', lambda s: 'or ', re.sub('(?<=\\s)&&\\s', lambda s: 'and ', input())) for _ in range(n)))))


import sys
import re
n = int(input())
for i in range(n):
	line = input()
	line = re.sub('(?<= )\\|\\|(?= )', 'or', line)
	line = re.sub('(?<= )&&(?= )', 'and', line)
	print(line)


import re
for _ in range(0, int(input())):
	line = input()
	chline1 = re.sub('(?<=[ ]{1})(&&{1})(?=[ ]{1})', 'and', line)
	chline2 = re.sub('(?<=[ ]{1})(\\|\\|{1})(?=[ ]{1})', 'or', chline1)
	print(chline2)


import re, sys
n = int(input())
for line in sys.stdin:
	remove_and = re.sub('(?<= )(&&)(?= )', 'and', line)
	remove_or = re.sub('(?<= )(\\|\\|)(?= )', 'or', remove_and)
	print(remove_or, end='')


import re
for _ in range(int(input())):
	line = re.sub(' && ', ' and ', input())
	line = re.sub(' && ', ' and ', line)
	line = re.sub(' \\|\\| ', ' or ', line)
	print(re.sub(' \\|\\| ', ' or ', line))


import re
numLines = int(input().strip())
for i in range(numLines):
	my_string = input()
	my_new_string = re.sub('\\s\\&\\&\\s', ' and ', my_string)
	while my_new_string != my_string:
		my_string = my_new_string
		my_new_string = re.sub('\\s\\&\\&\\s', ' and ', my_string)
	my_new_string = re.sub('\\s\\|\\|\\s', ' or ', my_string)
	while my_new_string != my_string:
		my_string = my_new_string
		my_new_string = re.sub('\\s\\|\\|\\s', ' or ', my_string)
	print(my_string)


import re
text = []
for _ in range(int(input())):
	text.append(input())
text = '\n'.join(text)
text = re.sub(' && ', ' and ', text)
text = re.sub(' && ', ' and ', text)
text = re.sub(' \\|\\| ', ' or ', text)
text = re.sub(' \\|\\| ', ' or ', text)
print(text)


import re
N = int(input())

def repl(match):
	if match.group(0) == '&&':
		return 'and'
	elif match.group(0) == '||':
		return 'or'
for _ in range(N):
	print(re.sub('((?<= )\\|\\|(?= ))|((?<= )&&(?= ))', repl, input()))


import re
num_lines = int(input())
text = [input() for x in range(1, num_lines + 1)]
patt_and = re.compile('(?<= )&&(?= )')
patt_or = re.compile('(?<= )\\|\\|(?= )')
text2 = '\n'.join(text)
text3 = patt_and.sub('and', text2)
text = patt_or.sub('or', text3)
print(text)


import re
for _ in range(int(input())):
	print(re.sub('(?<=\\s)(\\|\\||&&)(?=\\s)', lambda x: 'and' if x.group(1) == '&&' else 'or', input()))
print('\n')


import re

def orand(m):
	if m.group(0) == ' && ':
		return ' and '
	if m.group(0) == ' || ':
		return ' or '
for _ in range(int(input())):
	s = input()
	while ' && ' in s or ' || ' in s:
		s = re.sub(' (&&|\\|\\|) ', orand, s)
	print(s)


import re
text = ''
for _ in range(int(input())):
	text += input() + '\n'
print(re.sub('(?<=\\s)((&&)|(\\|\\|))(?=\\s)', lambda m: 'and' if m.group() == '&&' else 'or', text))


import re
for _ in range(int(input())):
	line = input()
	for _ in range(5):
		line = re.sub('\\s\\&\\&\\s', ' and ', line)
	for _ in range(5):
		line = re.sub('\\s\\|\\|\\s', ' or ', line)
	print(line)


import re
n = int(input())
for i in range(0, n):
	line = input()
	while line is not re.sub('\\s\\|\\|\\s', ' or ', line):
		line = re.sub('\\s\\|\\|\\s', ' or ', line)
	while line is not re.sub('\\s&&\\s', ' or ', line):
		line = re.sub('\\s&&\\s', ' and ', line)
	print(line)


import re
N = int(input())
c = re.compile('(?<= )&&(?= )', re.IGNORECASE)
for _ in range(N):
	p = re.sub(' \\|\\| ', ' or ', input())
	res = re.sub(c, 'and', p)
	print(res)


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


import re
for _ in range(int(input().strip())):
	print(re.sub('(?<= )\\|\\|(?= )', 'or', re.sub('(?<= )&&(?= )', 'and', input())))


import re
n = int(input())
for _ in range(n):
	i = input()
	ip = re.sub(' \\|\\|(?= )', ' or', i)
	ipp = re.sub(' &&(?= )', ' and', ip)
	print(ipp)


import re
for _ in range(int(input())):
	print(re.sub(' &&(?= )', ' and', re.sub(' \\|\\|(?= )', ' or', input())))


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


import re
n = int(input())
for i in range(n):
	k = input()
	while re.findall('( \\&\\& )', k):
		k = re.sub('( \\&\\& )', ' and ', k, 1)
	while re.findall('( \\|\\| )', k):
		k = re.sub('( \\|\\| )', ' or ', k, 1)
	print(k)


import re
t = int(input())
text = [input() for _ in range(t)]
reg_and = '( {1}&{2} {1})'
reg_or = '( {1}\\|{2} {1})'
for line in text:
	while line.find(' && ') >= 0:
		line = re.sub(reg_and, ' and ', line)
	while line.find(' || ') >= 0:
		line = re.sub(reg_or, ' or ', line)
	print(line)


import re
n = int(input())
for _ in range(n):
	s = input()
	s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
	s = re.sub('(?<= )\\&\\&(?= )', 'and', s)
	print(s)


import re

def andor(match):
	s = match.group(0)
	if '||' in s:
		return ' or'
	else:
		return ' and'
for line in range(int(input())):
	print(re.sub('(( \\|\\|)|( \\&\\&))(?= )', andor, input()))


import re
for _ in range(int(input().strip())):
	s = re.sub(' &&(?= )', ' and', input())
	s = re.sub(' \\|\\|(?= )', ' or', s)
	print(s)


import re
n = int(input())
lines = ''
for _ in range(n):
	lines += input() + '\n'
p = re.compile('(?<= )&&(?= )|(?<= )\\|\\|(?= )')
print(re.sub(p, lambda x: 'and' if x.group() == '&&' else 'or', lines))


import re
for i in range(int(input())):
	s = input()
	while s != re.sub('(\\s\\|\\|\\s)', ' or ', s):
		s = re.sub('(\\s\\|\\|\\s)', ' or ', s)
	while s != re.sub('(\\s&&\\s)', ' and ', s):
		s = re.sub('(\\s&&\\s)', ' and ', s)
	print(s)


import re
b = int(input())
for l in range(b):
	a = input()
	a = re.sub('(?<= )&&(?= )', 'and', a)
	a = re.sub('(?<= )\\|\\|(?= )', 'or', a)
	print(a)


import re

def symbol(match):
	if match.group(0) == '&&':
		return 'and'
	else:
		return 'or'
for i in range(int(input())):
	print(re.sub('((?<= )&&(?= ))|((?<= )\\|\\|(?= ))', symbol, input()))


from re import sub
for _ in range(int(input())):
	s = sub('( \\&\\&)+ ', lambda m: ' and' * (len(m.group(0)) // 3) + ' ', input())
	s = sub('( \\|\\|)+ ', lambda m: ' or' * (len(m.group(0)) // 3) + ' ', s)
	print(s)


import re
N = int(input())
s = '\n'.join([str(input()) for i in range(N)])
for i in range(2):
	s = re.sub(' \\&{2} ', ' and ', s)
	s = re.sub(' \\|{2} ', ' or ', s)
print(s)


import re
for _ in range(int(input())):
	s = re.sub('(?<= )&&(?= )', 'and', input())
	s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
	print(s)


import re
s = []

def change(match):
	if match.group(1) == '&':
		return 'and'
	else:
		return 'or'
for _ in range(int(input())):
	s.append(input())
	s[_] = re.sub('(?<=\\s)([&|])\\1{1}(?=\\s)', change, s[_])
[print(x) for x in s]


import re
for _ in range(int(input())):
	S = input()
	S = re.sub('(?<= )&&(?= )', 'and', S)
	S = re.sub('(?<= )\\|\\|(?= )', 'or', S)
	print(S)


import re
n = int(input())
for i in range(0, n):
	line = input()
	line = re.sub('(?<= )&&(?= )', 'and', line)
	line = re.sub('(?<= )\\|\\|(?= )', 'or', line)
	print(line)


import sys
import re

def repl(s):
	if s.group(0)[1] == '|':
		return 'or'
	return 'and'

def vn():
	num = int(sys.stdin.readline())
	while num:
		s = input()
		s = re.sub('(?<= )(\\|\\||&&)(?= )', repl, s)
		print(s)
		num -= 1
	return

def main():
	vn()
	return
main()


import re
for _ in range(int(input())):
	input_s = input()
	if '&&' in input_s.split():
		input_s = re.sub('(?<= )&&(?= )', 'and', input_s)
	if '||' in input_s.split():
		input_s = re.sub('(?<= )\\|\\|(?= )', 'or', input_s)
	print(input_s)


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


from re import sub
I = [input() for _ in range(int(input()))]
A = I
B = []
while A != B:
	B = A
	A = [sub(' && ', ' and ', x) for x in A]
	A = [sub(' \\|\\| ', ' or ', x) for x in A]
for x in A:
	print(x)


import re
b = int(input())
inputs = []
for _ in range(b):
	inputs.append(input())
finish = []
for line in inputs:
	line = re.sub('(?<= )&{2}(?= )', 'and', line, flags=re.IGNORECASE)
	line = re.sub('(?<= )\\|{2}(?= )', 'or', line, flags=re.IGNORECASE)
	print(line)


import re
n = int(input())
for i in range(n):
	s = input()
	s = re.sub('(?<= )&&(?= )', 'and', s)
	s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
	print(s)


import re
c = list()
a = int(input())
for i in range(a):
	c.append(input())
for i in c:
	i = re.sub('(?<= )&{2}(?= )', 'and', i)
	print(re.sub('(?<= )\\|{2}(?= )', 'or', i))


import re

def f(m):
	if m.group(0) == '||':
		return 'or'
	elif m.group(0) == '&&':
		return 'and'
for _ in range(int(input())):
	print(re.sub('(?<= )(\\|{2}|&{2})(?= )', f, input()))


for _ in range(int(input())):
	print(input().replace(' || ', ' or ').replace(' && ', ' and ').replace(' || ', ' or ').replace(' && ', ' and '))


import re

def fixLine(line):
	return re.sub('\\s(\\|\\|)(?=\\s)', ' or', re.sub('\\s(&&)(?=\\s)', ' and', line))
N = int(input())
print(*[fixLine(input()) for _ in range(N)], sep='\n')


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


import re

def switch(match):
	if match.group(0) == '&&':
		return 'and'
	else:
		return 'or'
for _ in range(int(input())):
	print(re.sub('(?<=\\s)\\&\\&(?=\\s)|(?<=\\s)\\|\\|(?=\\s)', switch, input()))


import re
__author__ = 'namii'
N = int(input())
for _ in range(N):
	text = input()
	r = re.sub('(?<= )&&(?= )', 'and', text)
	r = re.sub('(?<= )\\|\\|(?= )', 'or', r)
	print(r)


def sub(m):
	return m.group(0).replace('&&', 'and').replace('||', 'or')
import re
pattern = re.compile('(?<=\\s)(&&|\\|\\|)(?=\\s)')
for _ in range(int(input())):
	print(pattern.sub(sub, input()))


import re
n = int(input())

def convert(match):
	return match.group(0).replace('&&', 'and').replace('||', 'or')
for _ in range(n):
	line = input()
	print(re.sub('(?<= )(&&|\\|\\|)(?= )', convert, line))


import re
lst = []
or_ = lambda x: re.sub('\\ \\|\\|\\ ', ' or ', x)
and_ = lambda x: re.sub('\\ \\&\\&\\ ', ' and ', x)
n = int(input())
for i in range(n):
	lst.append(input())
for i in range(n):
	while re.search('\\ \\|\\|\\ ', lst[i]) or re.search('\\ \\&\\&\\ ', lst[i]):
		lst[i] = or_(lst[i])
		lst[i] = and_(lst[i])
	print(lst[i])


import re
text = ''
for _ in range(int(input())):
	text += input() + '\n'
text = re.sub('(?<= )&&(?= )', 'and', text)
text = re.sub('(?<= )\\|\\|(?= )', 'or', text)
print(text)


import re
N = int(input())
for _ in range(N):
	line = input()
	line = re.sub(' &{2} ', ' and ', line)
	line = re.sub(' \\|{2} ', ' or ', line)
	line = re.sub(' &{2} ', ' and ', line)
	line = re.sub(' \\|{2} ', ' or ', line)
	print(line)


import sys, re

def substitute(match):
	string = str(match.group())
	if string == '&&':
		return 'and'
	elif string == '||':
		return 'or'
n = int(input().strip())
for i in range(n):
	line = str(input())
	line = re.sub('(?<= )(\\&\\&|\\|\\|)(?= )', substitute, line)
	print(line)


import re
N = int(input())
for i in range(N):
	line = input()
	line = re.sub(' &&(?= )', ' and', line)
	line = re.sub(' \\|\\|(?= )', ' or', line)
	print(line)


import re

def replace_all(pattern, repl, string):
	while True:
		(aux, n) = re.subn(pattern, repl, string)
		if not n:
			return aux
		string = aux
n = int(input())
for i in range(n):
	s = input()
	s = replace_all(' && ', ' and ', s)
	s = replace_all(' \\|\\| ', ' or ', s)
	print(s)


import re
ampersands = re.compile(' && ')
bars = re.compile(' \\|\\| ')
for k in range(int(input())):
	s = input()
	while ampersands.findall(s):
		s = ampersands.sub(' and ', s)
	while bars.findall(s):
		s = bars.sub(' or ', s)
	print(s)


import re
N = input()
while True:
	try:
		S = input()
		S1 = re.sub('(?<= )\\&{2}(?= )', 'and', S)
		S2 = re.sub('(?<= )\\|{2}(?= )', 'or', S1)
		print(S2)
	except:
		break


import re
s = ''
for i in range(int(input())):
	s += input() + '\n'
s = re.sub('(?<= )\\&\\&(?= )', 'and', s)
s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
print(s)


import re
N = int(input())
for _ in range(N):
	s = input()
	s = re.sub('(?<=\\s)&&(?=\\s)', 'and', s)
	s = re.sub('(?<=\\s)\\|\\|(?=\\s)', 'or', s)
	print(s)


import re
n = int(input())
s = ''.join([input() + '\n' for _ in range(n)])
s = re.sub('(?<= )&&(?= )', 'and', s)
s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
print(s)


import re
import sys
rl = lambda : sys.stdin.readline()
n = int(rl())
txt = ''
for _ in range(n):
	txt += rl()
pattern1 = '(?<= )\\&\\&(?= )'
pattern2 = '(?<= )\\|\\|(?= )'
txt1 = re.sub(pattern1, 'and', txt)
txt2 = re.sub(pattern2, 'or', txt1)
print(txt2)


import re
N = int(input())
for _ in range(N):
	orsub = re.sub('\\s\\|\\|(?=\\s)', ' or', input())
	andsub = re.sub('\\s\\&\\&(?=\\s)', ' and', orsub)
	print(andsub)


import re
count = int(input())
for i in range(count):
	pat1 = re.sub('(?<=\\s{1})\\|\\|(?=\\s{1})', 'or', input())
	pat2 = re.sub('(?<=\\s{1})\\&\\&(?=\\s{1})', 'and', pat1)
	print(pat2)


import re
N = int(input())
for i in range(N):
	a = input()
	b = re.sub('(?<= )(&&)(?= )', 'and', a)
	c = re.sub('(?<= )(\\|\\|)(?= )', 'or', b)
	print(c)


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


from re import *
for i in range(int(input())):
	s = input()
	z = sub('(?<= )\\|\\|(?= )', 'or', s)
	p = sub('(?<= )\\&\\&(?= )', 'and', z)
	print(p)


import re
print('\n'.join([re.sub(' (&&|\\|\\|)(?= )', lambda x: ' and' if x.group(0)[1] == '&' else ' or', input()) for _ in range(int(input()))]))


import re
lista = []
for i in range(int(input())):
	s = input()
	s = re.sub('(?<= )\\&\\&(?= )', 'and', s)
	s = re.sub('(?<= )\\|\\|(?= )', 'or', s)
	lista.append(s)
print(*lista, sep='\n')


from re import sub
N = int(input())
s = ''
for _ in range(N):
	print(input().replace(' || ', ' or ').replace(' && ', ' and ').replace(' || ', ' or ').replace(' && ', ' and '))


import re
n = int(input())
text = ''
for i in range(n):
	text += input() + '\n'
text = re.sub(' \\&\\& ', ' and ', text)
text = re.sub(' \\|\\| ', ' or ', text)
text = re.sub(' \\&\\& ', ' and ', text)
text = re.sub(' \\|\\| ', ' or ', text)
print(text)


import re
no_lines = int(input().strip())
lines = []
for x in range(no_lines):
	lines.append(input().strip('\n'))
exp_and = re.compile('(?<= )\\&\\&(?= )')
exp_or = re.compile('(?<= )\\|\\|(?= )')
for line in lines:
	line = re.sub(exp_and, 'and', line)
	line = re.sub(exp_or, 'or', line)
	print(line)


import re
l = []
for _ in range(int(input())):
	l.append(input())
s = '\n'.join(l)

def replace(mObj):
	match = mObj.group(0)
	if match == '&&':
		return 'and'
	else:
		return 'or'
print(re.sub('(?<= )(&&|\\|\\|)(?= )', replace, s))


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


import re

def change(match):
	if match.group(0) == '&&':
		return 'and'
	else:
		return 'or'
for i in range(int(input())):
	line = input()
	print(re.sub('(?<= )(&&|\\|\\|)(?= )', change, line))


import re
n = int(input())
text = ''
for i in range(0, n):
	text = text + input() + '\n'
text = re.sub('( && )', ' and ', text)
text = re.sub('( \\|\\| )', ' or ', text)
text = re.sub('( && )', ' and ', text)
text = re.sub('( \\|\\| )', ' or ', text)
print(text)


import re
for _ in range(int(input())):
	line = input()
	line = re.sub('(?<= )&&(?= )', lambda x: 'and', line)
	line = re.sub('(?<= )\\|\\|(?= )', lambda x: 'or', line)
	print(line)


from re import sub
for i in range(int(input())):
	print(sub(' \\|\\|(?= )| \\&\\&(?= )', lambda x: ' or' if '||' in x.group(0) else ' and', input()))


import re
N = int(input())
for _ in range(N):
	s = re.sub('(?<=\\s)(\\|\\|)(?=\\s)', 'or', input())
	print(re.sub('(?<=\\s)(\\&\\&)(?=\\s)', 'and', s))


import re
n = int(input().strip())
for i in range(n):
	temp = re.sub('(?<= )&&(?= )', 'and', input())
	print(re.sub('((?<= )\\|\\|(?= ))', 'or', temp))


import re
N = int(input())
code = '\n'.join((input() for _ in range(N)))
code = re.sub('(?<= )&&(?= )', 'and', code)
code = re.sub('(?<= )\\|\\|(?= )', 'or', code)
print(code)


import re
nb = int(input())
for _ in range(nb):
	sg = input()
	sg = re.sub('(?<!^)(?<= )\\|\\|(?= )(?!$)', 'or', sg)
	sg = re.sub('(?<!^)(?<= )&&(?= )(?!$)', 'and', sg)
	print(sg)


import re
ii = int(input())
for i in range(0, ii):
	txt = input()
	txt = re.sub('\\ \\&\\&\\ ', ' and ', txt)
	txt = re.sub('\\ \\|\\|\\ ', ' or ', txt)
	txt = re.sub('\\ \\&\\&\\ ', ' and ', txt)
	txt = re.sub('\\ \\|\\|\\ ', ' or ', txt)
	print(txt)


import re

def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
n = read_int()
for i in range(n):
	line0 = input()
	line1 = line0 + 'X'
	while line0 != line1:
		line1 = line0
		line0 = re.sub('( && )', ' and ', line0)
	line1 = line0 + 'X'
	while line0 != line1:
		line1 = line0
		line0 = re.sub('( \\|\\| )', ' or ', line0)
	print(line0)


import re
for i in range(int(input())):
	a = input()
	print(re.sub('.* && .*', a.replace(' && ', ' and ').replace(' || ', ' or ').replace(' && ', ' and ').replace(' || ', ' or '), a) if ' && ' in a else re.sub('.* \\|\\| .*', a.replace(' || ', ' or ').replace(' && ', ' and ').replace(' && ', ' and ').replace(' || ', ' or '), a))


import re

def convert(match):
	if match.group(0) == '&&':
		return 'and'
	return 'or'
T = int(input())
for _ in range(T):
	s = input()
	print(re.sub('(?<= )((&&)|(\\|\\|))(?= )', convert, s))


a = int(input())
l = list()
m = list()
for i in range(a):
	s = input().split(' ')
	l.append(s)
for i in range(a):
	m.append('')
for i in range(a):
	m[i] += ' '.join(l[i]) + '\n'
s = ''.join(m)
countand = 0
countor = 0
for i in l:
	for j in i:
		if j == '&&':
			countand += 1
		elif j == '||':
			countor += 1
import re
q = re.sub(' && ', ' and ', s)
for i in range(countand - 1):
	q = re.sub(' && ', ' and ', q)
r = re.sub(' \\|\\| ', ' or ', q)
for i in range(countand - 1):
	r = re.sub(' \\|\\| ', ' or ', r)
print(r)


import re
n = int(input())
for i in range(n):
	print(re.sub('(?<=\\s)(\\|\\||&&)(?=\\s)', lambda m: {'||': 'or', '&&': 'and'}[m.group(0)], input()))


import re

def modify(m):
	s = m.group(0)
	s = re.sub('&&', 'and', s)
	return re.sub('\\|\\|', 'or', s)
for i in range(int(input())):
	s = re.sub('( &&| \\|\\|)+ ', modify, input())
	print(s)


import sys
import re
num = input()
lines = sys.stdin.read()
line1 = re.sub('(?<= )\\|\\|(?= )', 'or', lines)
print(re.sub('(?<= )&&(?= )', 'and', line1))


from re import sub
In = [input() for _ in range(int(input()))]
A = In
B = []
while A != B:
	B = A
	A = [sub(' && ', ' and ', x) for x in A]
	A = [sub(' \\|\\| ', ' or ', x) for x in A]
for x in A:
	print(x)


import re
repl = lambda x: 'or' if x.group(3) else 'and'
RE = re.compile('(?<=[ ])((&&)|(\\|\\|))(?=[ ])', re.VERBOSE)
for _ in range(int(input())):
	print(RE.sub(repl, input()))


import re
n = int(input())
m = []
for i in range(n):
	m.append(input())
	for j in range(5):
		m[i] = re.sub('\\s&&\\s', ' and ', m[i])
		m[i] = re.sub('\\s\\|\\|\\s', ' or ', m[i])
for line in m:
	print(line)


import sys
import re
input()
RE_A = '(?<=\\s)(\\&\\&)(?=\\s)'
RE_O = '(?<=\\s)(\\|\\|)(?=\\s)'
la = lambda _: 'and'
lo = lambda _: 'or'
text = ''.join(sys.stdin.readlines())
text = re.sub(RE_A, la, text)
text = re.sub(RE_O, lo, text)
print(text)


import re

def main():
	n = int(input())
	for _ in range(n):
		s = input()
		while True:
			t = s.replace(' && ', ' and ').replace(' || ', ' or ')
			if s == t:
				break
			s = t
		print(s)
main()


import re
n = int(input())
result = ''
if 0 < n < 100:
	for i in range(0, n):
		string = input()
		while re.search(' \\&\\& ', string):
			string = re.sub(' \\&\\& ', ' and ', string)
		while re.search(' \\|\\| ', string):
			string = re.sub(' \\|\\| ', ' or ', string)
		result += string + '\n'
	print(result)


import re
n = int(input())
for _ in range(n):
	line = input()
	line_with_double_ampersands_replaced = re.sub('(?<= )\\&\\&(?= )', 'and', line)
	line_with_double_ampersands_and_bars_replaced = re.sub('(?<= )\\|\\|(?= )', 'or', line_with_double_ampersands_replaced)
	print(line_with_double_ampersands_and_bars_replaced)


import re
n = int(input())
for _ in range(n):
	line = input()
	(line, count) = re.subn(' \\|\\| ', ' or ', line)
	while count > 0:
		(line, count) = re.subn(' \\|\\| ', ' or ', line)
	(line, count) = re.subn(' \\&\\& ', ' and ', line)
	while count > 0:
		(line, count) = re.subn(' \\&\\& ', ' and ', line)
	print(line)


import re
regex1 = ' &&(?= )'
regex2 = ' \\|\\|(?= )'
n = int(input())
for i in range(n):
	line = str(input())
	matches1 = re.finditer(regex1, line)
	line = re.sub(regex1, ' and', line)
	line = re.sub(regex2, ' or', line)
	print(line)