Strings Flashcards
(19 cards)
Are strings mutable or immutable?
Strings are immutable in Python. This means that once a string has been defined, it can’t be change.
Can you use for in a string?
Yes, the same way as in list. str=”hello” for letter in str:
print(letter)
Como es the in syntax?
The in syntax se usa para determinar si a letter o a substring esta en un string. Devuelve verdadero o false.
Ejemplo
game=’claro que si’
print(‘que’ in game) ==> imprime True
print(‘on’ in game)==> imprime False
Como pones una string en minusculas?
con el metodo .lower() str=’Let Me Know’ str.lower() ===> let me know
How can you know the lenght of a string?
The same way as list, with len str=’hello’ len(str) ==> es 5
How can you use the split method?
The split method turns a string into a list.
If no argument is passed, the default behaviour is to split on whitespace.
if an argument is passed to the method, that values is used as the delimiter on which to split the string.
Si quiero dividir la string en caracteres no utilizo split sino list: list(str) y eso me devuelve un lista con los caracteres del string.
Ejemplo
str1=”Claro que si”
str1.split() ==> [‘Claro’, ‘que’, ‘si’]
str2=’Silicon Valley’
str2.split(‘i’) ==> [‘S’, ‘l’, ‘con Valley’]
How do you check if a string ends with a specific character?
s.endswith(‘!’)
How do you concatenate strings?
With the + greeting=”hello “ name=’victoria’ full_greeting=greeting+name ==> hello victoria
How do you concatenate the values of a list into a string?
With the join() method. Ejemplo str=”-“.join([‘I’, ‘am’, ‘awesome’]) ==> str es igual a ‘I-am-awesome’
How do you escape characters?
With the \
Ejemplo:
txt=”She said "Have some pie".”
Lo que sucede es que si quiero que tome las comillas como parte del string tengo que escaparlo, para eso se usa \ .Así toma las comillas como dentro del string.
How do you put all the string in capital letter?
With the upper method. Ejemplo: dino=’t-rex’ dino.upper() ==> T-REX
How do you remove white spaces in a string?
With the method .strip(). It can be use to remove characters from the beginning and the end.
Ejemplo:
text1=” apples “
text1.strip() ==> “apples”
También sirve para remover otro tipo de caracteres indicando cual entre paréntesis.
Ejemplo:
text2=”….oranges….”
text2.strip(‘.’) ===> “oranges”
text3=”….+….pears…..-“
text3.strip(‘.+-‘) ===> “pears”
How do you turn a string into a title capitalization?
With .title() str=”capitan marvel” str.title() ==> “Capitan Marvel”
How do you use the string method .format() ?
Sirve para reemplazar {} de una string con los valores que se le pase al método como argumento. .format devuelve una copia, no modifica el original.
Ejemplo:
msj=”Quedan por hacer {} de {} ejercicio/s” mensaje=msj.format(2,10) ==> mensaje dice: Quedan por hacer 2 de 10 ejercicio/s También se puede usar placeholders msj=”Yo naci el dia {day} del mes de {month} del año {year}”
msj.format(year=1990,month=’marzo’,day=29) ==> Yo naci el dia 29 del mes de marzo del año 1990.
What is a string?
In computer science, a string is a sequence of characters. They can be any length and may include spaces, letters, numbers and symbols.
What is an IndexError?
Es cuando intentamos acceder a un índice que no existe. Por ejemplo fruta=”Berry” i=fruta[6] va a dar error porque fruta solo tiene índices del cero al 4 inclusive.
What is the method .find() for?
The Python string method .find() devuelve el index of the first ocurrence of the string passed as the argument. Devuelve -1 si no lo encuentra. str=’Mountain’ str.find(‘o’) ==> devuelve 1 porque la o esta en el indice 1.
What is the replace method and how do you use it?
The replace() method is used to replace the occurence of the first argument with the second argument within the string. No modifica el original, sino que devuelve una copia que puedo guardar en una variable. Ejemplo: str=’strawberry’ str.replace(‘r’,’R’) ==> stRawbeRRy
indexing and slicing strings?
El index de los caracteres de un string son igual que en una list: van desde cero. Entonces str=’yellow’ str[0] ===> va a ser ‘y’ También se puede slice the string como con las listas: str[4:6] ===> va a ser ‘ow’