-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodulo-string.py
More file actions
40 lines (32 loc) · 1.16 KB
/
modulo-string.py
File metadata and controls
40 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
El módulo de 'string' proporciona una serie de constantes muy útiles para manejo de strings, a demás de distintas estrategias de formateado de cadenas de texto.
"""
"""
CONSTANTES:
"""
import string
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.ascii_letters)
print(string.digits)
print(string.hexdigits)
print(string.octdigits)
print(string.punctuation)
print(string.whitespace)
print(string.printable)
"""
PLANTILLAS: El módulo string también proporciona plantillas con interpolación de variables. Algo similar a las f-strings.
"""
from string import Template
template1 = Template("$lang is the best programming language in the $place!")
# Realizando las sustituciones
print(template1.substitute(lang="Python", place="Earth"))
# También se puede pasar con un diccionario
print(template1.substitute({"lang": "Python", "place": "Earth"}))
# Utilizar llaves para evitar la ambiguedad
template2 = Template("You won several ${price}s")
print(template2.substitute(price="phone"))
urlBase = Template("https://python.org/3/library/$module.html")
for module in ("string", "re", "difflib"):
url = urlBase.substitute(module=module)
print(url)