Ejemplos funciones

some_string = 'Hello World'
print('Testing a String')
print(some_string[2])
print(some_string[2:5])
print(some_string[2:])
print(some_string[:-2])

print('-|-' * 20)
print('some_string', some_string)
print("some_string.startswith('H')",
some_string.startswith('H'))
print("some_string.startswith('h')",
some_string.startswith('h'))
print("some_string.endswith('d')", some_string.endswith('d'))
print('some_string.istitle()', some_string.istitle())
print('some_string.isupper()', some_string.isupper())
print('some_string.islower()', some_string.islower())
print('some_string.isalpha()', some_string.isalpha())
print('String conversions')
print('-' * 20)
print('some_string.upper()', some_string.upper())
print('some_string.lower()', some_string.lower())
print('some_string.title()', some_string.title())
print('some_string.swapcase()', some_string.swapcase())
print('String leading, trailing spaces', " xyz ".strip())
cadena="   hola que tal     "
print("|"+cadena+"|")
print("|"+cadena.strip()+"|")

clientes=["ana pi   ","PEDRO","JUAN perez    "]

for cliente in clientes:
    print("|"+cliente.strip().title()+"|")

cadena="hola que tal"

pos=cadena.find("pepe")
pos2=cadena.find("que")

print(pos,pos2)

pos3=cadena.find("a")
print(pos3)
pos4=cadena.find("a",pos3+1)
print(pos4)

cadena="Mañana cantara Sara"

pos=cadena.find("w")
while pos!=-1:
    print(pos)
    pos=cadena.find("a",pos+1)

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos