Strings - Cadenas de Texto

Veamos algunos ejemplos:

prog/str01.py

Python 3.4.2 (default, Oct  8 2014, 13:14:40)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> p = "casa"
>>> q = 'perro'
>>> p + q
'casaperro'
>>> p + " " + q
'casa perro'
>>> p * 3
'casacasacasa'
>>> p * 3 + q
'casacasacasaperro'
>>> (p + "!") * 2 + q
'casa!casa!perro'
>>> p = 485.58
>>> type(p)
<class 'float'>
>>> r = str(p)
>>> type(r)
<class 'str'>
>>>
>>>
>>>
>>>
>>>
>>>
>>> r
'485.58'
>>> str(124.56)
'124.56'
>>>
>>>
>>>
>>>
>>>

prog/str02.py

Python 3.4.2 (default, Oct  8 2014, 13:14:40)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> s = "una frase cualquiera"
>>> s[0]
'u'
>>> s[1]
'n'
>>> s[2]
'a'
>>> s[]
SyntaxError: invalid syntax
>>> s[3]
' '
>>> s[4]
'f'
>>> s[0:3]
'una'
>>> s[4:9]
'frase'
>>> s[4:10]
'frase '
>>>
>>>
>>>
>>> ================================ RESTART ================================
>>> s = "una frase cualquiera"
>>> s[:3]
'una'
>>>
>>>
>>>
>>>
>>> s[10:]
'cualquiera'
>>>
>>>
>>>
>>>
>>> len(s)
20
>>>
>>>
>>>
>>>
>>>

prog/str03.py

a = "el dia esta nublado"

for letra in a:
    print(letra.upper())

prog/str04.py

##desde = int(input("numero desde"))
##hasta = int(input("numero final"))
##
##for num in range(desde, hasta+1):
##    print(num)
##
##

mul = int(input("tabla del numero: "))

for num in range(0,11):
    print("%2d * %2d = %3d" % (num, mul, num * mul))

prog/prueba.py

#!/usr/bin/env python3

# funcion que calcula el promedio
# de dos numeros
def promedio(numero1, numero2):
    prom = (numero1 + numero2) / 2

    return prom

print(promedio(8, 5))

p = int(input("ingrese un num: " ))
q = int(input("ingrese otro num: " ))

t = promedio(p, q)

print(t)

print(promedio(promedio(10,20), promedio(30,40)))