Algunos de los métodos de string están cubiertos en los siguientes conjuntos.
Métodos de strings, parte 1
Métodos de strings, parte 2
En este artículo se analizan más métodos
1. strip() : este método se usa para eliminar todos los caracteres iniciales y finales mencionados en su argumento.
2. lstrip() :- Este método se usa para eliminar todos los caracteres principales mencionados en su argumento.
3. rstrip() :- Este método se usa para eliminar todos los caracteres finales mencionados en su argumento.
Python
# Python code to demonstrate working of # strip(), lstrip() and rstrip() str = "---geeksforgeeks---" # using strip() to delete all '-' print ( " String after stripping all '-' is : ", end="") print ( str.strip('-') ) # using lstrip() to delete all trailing '-' print ( " String after stripping all leading '-' is : ", end="") print ( str.lstrip('-') ) # using rstrip() to delete all leading '-' print ( " String after stripping all trailing '-' is : ", end="") print ( str.rstrip('-') )
Producción:
Python
# Python code to demonstrate working of # min() and max() str = "geeksforgeeks" # using min() to print the smallest character # prints 'e' print ("The minimum value character is : " + min(str)) # using max() to print the largest character # prints 's' print ("The maximum value character is : " + max(str))
Python
# Python code to demonstrate working of # maketrans() and translate() from string import maketrans # for maketrans() str = "geeksforgeeks" str1 = "gfo" str2 = "abc" # using maketrans() to map elements of str2 with str1 mapped = maketrans( str1, str2 ) # using translate() to translate using the mapping print "The string after translation using mapped elements is : " print str.translate(mapped)
Python
# Python code to demonstrate working of # replace() str = "nerdsfornerds is for nerds" str1 = "nerds" str2 = "geeks" # using replace() to replace str2 with str1 in str # only changes 2 occurrences print ("The string after replacing strings is : ", end="") print (str.replace( str1, str2, 2))
Python3
# Python code to illustrate expandtabs() string = 'GEEKS\tFOR\tGEEKS' # No parameters, by default size is 8 print (string.expandtabs()) # tab size taken as 2 print(string.expandtabs(2)) # tab size taken as 5 print(string.expandtabs(5))
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA