In [1]:
s = 'one two one two one'

In [2]:
print(s.replace(' ', '-'))


one-two-one-two-one

In [3]:
print(s.replace(' ', ''))


onetwoonetwoone

In [4]:
print(s.replace('one', 'XXX'))


XXX two XXX two XXX

In [5]:
print(s.replace('one', 'XXX', 2))


XXX two XXX two one

In [6]:
print(s.replace('one', 'XXX').replace('two', 'YYY'))


XXX YYY XXX YYY XXX

In [7]:
print(s.replace('one', 'XtwoX').replace('two', 'YYY'))


XYYYX YYY XYYYX YYY XYYYX

In [8]:
print(s.replace('two', 'YYY').replace('one', 'XtwoX'))


XtwoX YYY XtwoX YYY XtwoX

In [9]:
s_lines = 'one\ntwo\nthree'
print(s_lines)


one
two
three

In [10]:
print(s_lines.replace('\n', '-'))


one-two-three

In [11]:
s_lines_multi = 'one\ntwo\r\nthree'
print(s_lines_multi)


one
two
three

In [12]:
print(s_lines_multi.replace('\r\n', '-').replace('\n', '-'))


one-two-three

In [13]:
print(s_lines_multi.replace('\n', '-').replace('\r\n', '-'))


-three

In [14]:
print(s_lines_multi.splitlines())


['one', 'two', 'three']

In [15]:
print('-'.join(s_lines_multi.splitlines()))


one-two-three

In [16]:
s = 'one two one two one'

In [17]:
print(s.translate(str.maketrans({'o': 'O', 't': 'T'})))


One TwO One TwO One

In [18]:
print(s.translate(str.maketrans({'o': 'XXX', 't': None})))


XXXne wXXX XXXne wXXX XXXne

In [19]:
print(s.translate(str.maketrans('ow', 'XY', 'n')))


Xe tYX Xe tYX Xe

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:])


abcdXXXhij

In [23]:
s_replace = 'XXX'
i = 4

In [24]:
print(s[:i] + s_replace + s[i + len(s_replace):])


abcdXXXhij

In [25]:
print(s[:4] + '-' + s[7:])


abcd-hij

In [26]:
print(s[:4] + '+++++' + s[4:])


abcd+++++efghij