Python functionreplace()
its definition
Returns a copy of the text that called it with the replacement of every part in it that matches the text, we pass it to it in the place of the parameter with the oldtext that we pass to it in the place of the parameter new.
built
str.replace(old, new [, count])
parameters
In place of the parameter
oldwe pass the text that we want to replace in the original text.Parameter location We
newpass a text that will replace it in the copy that you will return.The parameter
countis an optional parameter, you can pass in its place a number greater than zero that represents the first number of parts found that you want to change.
Return value
Returns a copy of the text that called it with the replacement of every part in it that matches the text, we pass it to it in the place of the parameter with the oldtext that we pass to it in the place of the parameter new.
first example
# s Here we have defined a text variable named
s = 'java is easy to learn. anyone can learn java.'
# As is s here we have printed the text in the variable
print(s)
# 'python' with the word s I found in the variable 'java' after replacing each word replace() here we print what the function will return
print(s.replace('java', 'python'))
• We will get the following result when running.
python is easy to learn. anyone can learn python.
second example
# s Here we have defined a text variable named
s = 'java is easy to learn. anyone can learn java.'
# As is s here we have printed the text in the variable
print(s)
# 'python' with the word s I found in the variable 'java' after I replaced the first replace() word here we print what the function will return
print(s.replace('java', 'python', 1))
• We will get the following result when running.
python is easy to learn. anyone can learn java.
Python functionmaketrans()
its definition
Used to build a dictionary that can be used to replace text characters with other characters.
Note: To apply the dictionary you have prepared to any text, you need to use the translate().
built
static str.maketrans(x[, y[, z]])
parameters
In place of the parameter
xwe pass the character or set of characters that we want to replace with new characters.
Also, you can pass to pass the characters you want to replace and the characters you want to replace them with.The parameter
yis an optional parameter, we pass the character or group of characters we want to put in place of the characters we passed in place of the parameterx.The parameter
zis an optional parameter, you can pass in its place any character or group of characters you want to delete.
Return value
Returns a dictionary of its type tablethat can be used to replace text characters with other characters.
first example
s = 'alawirisaddam.com' # s Here we have defined a text variable named
dictionary = str.maketrans('a', '-') # '-' with 'a' when translating with this dictionary every letter will be replaced.dictionary Here we have created a dictionary for translation named
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
second example
s = 'alawirisaddam.com' # s Here we have defined a text variable named
dictionary = str.maketrans('am', '-*') # dictionary Here we have created a translation dictionary called
# '-' with 'a' when translating with this dictionary each letter will be replaced
# '*' with 'm' and each character will be swapped
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
-l-wiris-dd-*.co*
third example
s = 'alawirisaddam.com' # s Here we have defined a text variable named
dictionary = str.maketrans('a', '-', 'm') # dictionary Here we have created a translation dictionary named
# '-' with 'a' when translating with this dictionary each letter will be replaced
# 'm' is found and every letter will be deleted
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
Fourth example
s = 'alawiri.com' # s Here we have defined a text variable named
dictionary = str.maketrans({ # dictionary Here we have created a dictionary for translation called
'a': 'XYZ', # 'XYZ' with letters 'a' When translating with this dictionary each letter will be replaced
'r': '555', # '555' with 'r' and each character will be replaced
'.' : '' # Also, every dot found will be deleted
})
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) # s on the variable table after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
xyzlxyzwi555com
Python functiontranslate()
its definition
Returns a copy of the text that called it with the replacement of some letters of this copy with other letters or its deletion depending on the dictionary that we pass to it in place of the parameter table.
Note: The dictionary that we pass to this function is basically built by the function translate().
built
str.translate(table)
parameters
Parameter location We
tablepass an object of typetablethat represents the dictionary that the function will use when modifying the content of the text it returns.
Return value
Returns a copy of the text that called it with the replacement of some letters of this copy with other letters or its deletion depending on the dictionary that we pass to it in place of the parameter table.
first example
s = 'alawiri.com' # s Here we have defined a text variable named
dictionary = str.maketrans('a', '-') # '-' with 'a' when translating with this dictionary every letter will be replaced.dictionary Here we have created a dictionary for translation named
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
-l-wiri.com
second example
s = 'alawiri.com' # s Here we have defined a text variable named
dictionary = str.maketrans('am', '-*') # dictionary Here we have created a translation dictionary called
# '-' with 'a' when translating with this dictionary each letter will be replaced
# '*' with 'm' and each character will be swapped
# 'o' is found and every letter will be deleted
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
*
third example
s = 'alawiri.com' # s Here we have defined a text variable named
dictionary = str.maketrans('a', '-', 'm') # dictionary Here we have created a translation dictionary named
# '-' with 'a' when translating with this dictionary each letter will be replaced
# 'm' is found and every letter will be deleted
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) #s on the dictionary variable after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
-l-wiri.co
Fourth example
s = 'alawiri.com' # s Here we have defined a text variable named
dictionary = str.maketrans({ # dictionary Here we have created a dictionary for translation called
'a': 'XYZ', # 'XYZ' with letters 'a' When translating with this dictionary each letter will be replaced
'r': '555', # '555' with 'r' and each character will be replaced
'.' : '' # Also, every dot found will be deleted
})
print(s) # as s Here we have printed the text in the variable
print(s.translate(dictionary)) # s on the variable table after applying the translation stored in the dictionary translate() here we print what the function will return
• We will get the following result when running.
XYZlxyzwi555icom