Min menu

Pages

Toggle functions in the str class for text handling in Python

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

Test.py
                    # 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.

java is easy to learn. anyone can learn java.
python is easy to learn. anyone can learn python.


second example

Test.py
                    # 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.

java is easy to learn. anyone can learn java.
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 parameter x.

  • 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

Test.py
                    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.

alawirisaddam.com
-l-wiris-dd-m.com

second example

Test.py
                    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.

alawirisaddam.com
-l-wiris-dd-*.co*


third example

Test.py
                    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.

alawirisaddam.com
-l-wiris-dd-.co

Fourth example

Test.py
                    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.

alawiri.com
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 type tablethat 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

Test.py
                    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.

alawiri.com
-l-wiri.com


second example

Test.py
                    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.

alawiri.com-l-wiri.co
*


third example

Test.py
                    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.

alawiri.com
-l-wiri.co


Fourth example

Test.py
                    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.

alawiri.com
XYZlxyzwi555icom