In [1]:
s = 'one two one two one'
In [2]:
print(s.replace(' ', '-'))
In [3]:
print(s.replace(' ', ''))
In [4]:
print(s.replace('one', 'XXX'))
In [5]:
print(s.replace('one', 'XXX', 2))
In [6]:
print(s.replace('one', 'XXX').replace('two', 'YYY'))
In [7]:
print(s.replace('one', 'XtwoX').replace('two', 'YYY'))
In [8]:
print(s.replace('two', 'YYY').replace('one', 'XtwoX'))
In [9]:
s_lines = 'one\ntwo\nthree'
print(s_lines)
In [10]:
print(s_lines.replace('\n', '-'))
In [11]:
s_lines_multi = 'one\ntwo\r\nthree'
print(s_lines_multi)
In [12]:
print(s_lines_multi.replace('\r\n', '-').replace('\n', '-'))
In [13]:
print(s_lines_multi.replace('\n', '-').replace('\r\n', '-'))
In [14]:
print(s_lines_multi.splitlines())
In [15]:
print('-'.join(s_lines_multi.splitlines()))
In [16]:
s = 'one two one two one'
In [17]:
print(s.translate(str.maketrans({'o': 'O', 't': 'T'})))
In [18]:
print(s.translate(str.maketrans({'o': 'XXX', 't': None})))
In [19]:
print(s.translate(str.maketrans('ow', 'XY', 'n')))
In [20]:
# print(s.translate(str.maketrans('ow', 'XXY', 'n')))
# ValueError: the first two maketrans arguments must have equal length
In [21]:
s = 'abcdefghij'
In [22]:
print(s[:4] + 'XXX' + s[7:])
In [23]:
s_replace = 'XXX'
i = 4
In [24]:
print(s[:i] + s_replace + s[i + len(s_replace):])
In [25]:
print(s[:4] + '-' + s[7:])
In [26]:
print(s[:4] + '+++++' + s[4:])