Skip to content

Commit 2259d6c

Browse files
committed
#4 - Python
1 parent 2254edb commit 2259d6c

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 04
2+
'''
3+
Operaciones con cadenas de caracteres
4+
'''
5+
6+
# Creacion y concatenacion
7+
a = "Hola"
8+
b = "Mundo"
9+
c = a + " " + b # Concatenación → 'Hola Mundo'
10+
d = a * 3 # Repetición → 'HolaHolaHola'
11+
12+
13+
# Indexación y segmentación (slicing)
14+
texto = "Python"
15+
texto[0] # 'P' → primer carácter
16+
texto[-1] # 'n' → último carácter
17+
texto[0:3] # 'Pyt' → desde 0 hasta antes del 3
18+
texto[2:] # 'thon' → desde 2 hasta el final
19+
texto[:4] # 'Pyth' → desde el inicio hasta antes del 4
20+
texto[::-1] # 'nohtyP' → invertir cadena
21+
22+
23+
#Búsqueda y verificación
24+
texto = "programar en python"
25+
26+
"python" in texto # True → verifica si existe
27+
"java" not in texto # True → verifica si no existe
28+
texto.find("python") # 13 → índice donde empieza
29+
texto.rfind("n") # última aparición de 'n'
30+
texto.startswith("pro") # True
31+
texto.endswith("on") # True
32+
33+
# Modificación de contenido
34+
texto = "hola mundo"
35+
36+
texto.upper() # 'HOLA MUNDO'
37+
texto.lower() # 'hola mundo'
38+
texto.title() # 'Hola Mundo'
39+
texto.capitalize() # 'Hola mundo'
40+
print(texto.swapcase()) # 'HOLA MUNDO' → 'hola mundo'
41+
texto.replace("mundo", "Python") # 'hola Python'
42+
43+
# Eliminación de espacios o caracteres
44+
texto = " hola mundo "
45+
46+
texto.strip() # 'hola mundo' → elimina espacios a ambos lados
47+
texto.lstrip() # 'hola mundo ' → elimina solo a la izquierda
48+
texto.rstrip() # ' hola mundo' → elimina solo a la derecha
49+
texto.strip("o") # elimina las 'o' de los extremos → ' hola mund'
50+
51+
52+
#División y unión
53+
texto = "uno,dos,tres"
54+
55+
texto.split(",") # ['uno', 'dos', 'tres'] → divide por comas
56+
texto.split() # divide por espacios
57+
"-".join(["uno", "dos", "tres"]) # 'uno-dos-tres' → une con '-'
58+
59+
# Información de la cadena
60+
texto = "Python"
61+
62+
len(texto) # 6 → longitud
63+
texto.count("t") # 1 → cuántas veces aparece
64+
65+
# Comprobaciones de tipo
66+
"abc".isalpha() # True → solo letras
67+
"123".isdigit() # True → solo dígitos
68+
"abc123".isalnum() # True → letras y números
69+
"hola mundo".isspace() # False → solo espacios
70+
"HOLA".isupper() # True
71+
"hola".islower() # True
72+
"Hola Mundo".istitle() # True
73+
74+
# Alineación y formato
75+
texto = "Python"
76+
77+
texto.center(10, "-") # '--Python--'
78+
texto.ljust(10, ".") # 'Python....'
79+
texto.rjust(10, ".") # '....Python'
80+
81+
# Formato de cadenas (interpolación)
82+
nombre = "Brian"
83+
edad = 24
84+
85+
# Forma moderna (f-string)
86+
f"Hola {nombre}, tienes {edad} años"
87+
88+
# Método format
89+
"Hola {}, tienes {} años".format(nombre, edad)
90+
91+
# Índices
92+
"Hola {0}, tienes {1} años".format(nombre, edad)
93+
94+
# Diccionario
95+
"Hola {n}, tienes {e} años".format(n=nombre, e=edad)
96+
97+
# Métodos útiles adicionales
98+
texto = "Hola mundo"
99+
100+
texto.encode() # convierte a bytes
101+
texto.expandtabs() # reemplaza tabulaciones con espacios
102+
texto.partition(" ") # ('Hola', ' ', 'mundo') → divide en 3 partes
103+
texto.rpartition(" ") # divide desde el final
104+
texto.zfill(10) # '000Hola mundo' → rellena con ceros

0 commit comments

Comments
 (0)