#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목Python에서는 C/C++처럼 문자열에 [i]로 값을 줄수 없어요.2019-04-06 22:49
작성자 Level 10

1. Python에서의 String은 immutable, 즉 수정이 불가능합니다. 아래는 이에 대한 python.org의 내용입니다.


Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:

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

If you need a different string, you should create a new one:

>>>
>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'
 


2. 따라서, 아래와 같은 형태로 해야 하는 것이지요.


def reverseString(strr):
b = strr[4]
b += strr[3]
b += strr[2]
b += strr[1]
b += strr[0]
return b

print( reverseString("Hello") )
댓글
이전문자열 뒤집기 질문2019-04-06
다음VSC 돌아가는건가요?2019-04-06