May 2019
Beginner
528 pages
29h 51m
English
A common text manipulation is to locate a substring and replace its value. Method replace takes two substrings. It searches a string for the substring in its first argument and replaces each occurrence with the substring in its second argument. The method returns a new string containing the results. Let’s replace tab characters with commas:
In [1]: values = '1\t2\t3\t4\t5'In [2]: values.replace('\t', ',')Out[2]: '1,2,3,4,5'
Method replace can receive an optional third argument specifying the maximum number of replacements to perform.
(IPython Session) Replace the spaces in the string '1 2 3 4 5' with ...
Read now
Unlock full access