Curso de Programación en Python/Str1

De WikiCabal
Saltar a: navegación, buscar

Str1.py

Las strings, en Python, son inmutables

[rrc@pridd ~]$ python3

Python 3.3.2 (default, Jun 28 2014, 20:02:44) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Str = "Hala Mundo"
>>> Str[3]
'a'
>>> Str[3] = 'o'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 
#!/usr/bin/python3
#-*-coding: utf-8 -*-

Frase = "Hola Mundo"
i = 0

while Frase[i]:
  print( Frase[i] )
  i += 1
print( "\nListo\n" )

Resultado

[rrc@Llawyr PythonClase]$ ./Str1.py

H
o
l
a

M
u
n
d
o
Traceback (most recent call last):
  File "./Str1.py", line 7, in <module>
    while Frase[i]:
IndexError: string index out of range

Explicación