Curso de Programación en Python/Str1

De WikiCabal
Ir a la navegación Ir a la búsqueda

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
>>> 
 1 #!/usr/bin/python3
 2 #-*-coding: utf-8 -*-
 3 
 4 Frase = "Hola Mundo"
 5 i = 0
 6 
 7 while Frase[i]:
 8   print( Frase[i] )
 9   i += 1
10 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