Pythonの文字列

投稿者: | 2月 23, 2021

Pythonの文字列は不変体(immutable)であるため、文字列のインデックス位置に代入してもエラーが返る。

>>> word
'Python'
>>> word[0]
'P'
>>> word[0] = 's'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 
>>> 'J' + word[1:]
'Jython'
>>> 'P' + word[1:]
'Python'
>>>