10. Strings str
En esta sección veremos con mas detalle la manipulación de strings.
La manipulación de string a detalle es sumamente util para separarlo, saber si contiene alguna letra, palabra u oración. Recordemos que todo en python es un objeto y al ser un objeto contienen por default muchos métodos con los cuales podemos operar ese string
.
10.1 Convertir a minúsculas (lower)
Una función que nos facilita es convertir todo el string a minúsculas. Ignora simbolos y números.
- lower(): Converts a string into lower case
# Todas la letras las pasara a minúsculas
mensaje = "Hola Como ESTAS"
minuscula = mensaje.lower()
print(mensaje)
print(minuscula)
Hola Como ESTAS
hola como estas
10.2 Convertir a Mayúsculas (upper)
Una función que nos facilita es convertir todo el string a mayúsculas. Ignora símbolos y números.
- upper(): Converts a string into upper case
# Todas la letras las pasara a mayúsculas
mensaje = "Hola Como ESTAS"
upper = mensaje.upper()
print(mensaje)
print(upper)
Hola Como ESTAS
HOLA COMO ESTAS
10.3 Convertir a Mayúsculas (capitalize)
Una función que nos facilita es convertir solo la primer letra a mayúsculas, el resto a minúsculas. Esto es útil para cuando queremos que una oración inicie en mayúsculas o palabra. Ignora símbolos y números.
capitalize()
: Converts the first character to upper case
# Todas la letras las pasara a mayúsculas
mensaje = "hola cOMO ESTAS"
capital = mensaje.capitalize()
print(mensaje)
print(capital)
hola cOMO ESTAS
Hola como estas
10.4 Contador de palabras (count)
Los string cuenta con método el cual nos ayuda directamente a saber cuantas veces se encuentra una palabra dentro de ese string, debe ser igual la palabra a la que se le paso.
- count(value, start, end): method returns the number of times a specified value appears in the string
parámetros:
value
: Required. A String. The string to value to search forstart
: Optional. An Integer. The position to start the search. Default is 0end
: Optional. An Integer. The position to end the search. Default is the end of the string
phrase = "Ingeniería en Sistemas es lo mejor del mundo, tienes el mundo en tus manos"
count = phrase.count("mundo")
print(f'Veces que sale la palabra \"mundo\" es: {count}')
Veces que sale la palabra "mundo" es: 2
10.5 Es o no es un
En ocasiones queremos conocer si un string es o esta en cierta forma.
isdigit()
: Returns True if all characters in the string are digitsislower()
: Returns True if all characters in the string are lower caseisnumeric()
: Returns True if all characters in the string are numericisspace()
: Returns True if all characters in the string are whitespacesisupper()
: Returns True if all characters in the string are upper case
print(f'Es un número: {"4".isdigit()}')
print(f'Es un número: {"4".isnumeric()}')
print(f'Esta en minúsculas: {"hola".islower()}')
print(f'Esta en mayúsculas: {"HOLA".isupper()}')
print(f'Son espacios: {" ".isspace()}')
Es un número: True
Es un número: True
Esta en minúsculas: True
Esta en mayúsculas: True
Son espacios: True
endswith(value)
Returns true if the string ends with the specified valuetitle()
Converts the first character of each word to upper casefind()
Searches the string for a specified value and returns the position of where it was foundindex()
Searches the string for a specified value and returns the position of where it was foundformat()
Formats specified values in a stringjoin()
Joins the elements of an iterable to the end of the stringlstrip()
Returns a left trim version of the stringreplace()
Returns a string where a specified value is replaced with a specified valuerfind()
Searches the string for a specified value and returns the last position of where it was foundrindex()
Searches the string for a specified value and returns the last position of where it was foundrsplit()
Splits the string at the specified separator, and returns a listrstrip()
Returns a right trim version of the stringsplit()
Splits the string at the specified separator, and returns a listsplitlines()
Splits the string at line breaks and returns a liststartswith()
Returns true if the string starts with the specified valuestrip()
Returns a trimmed version of the string
https://www.w3schools.com/python/python_ref_string.asp
Realizado por el Instructor: Alejandro Leyva