비어 있지 않은 문자열에서 특정 인덱스 문자를 제거해야 하는 경우 반복할 수 있으며 인덱스가 일치하지 않는 경우 해당 문자를 다른 문자열에 저장할 수 있습니다.
아래는 동일한 데모입니다 -
예시
my_string = "Hi there how are you"
print("The string is :")
print(my_string)
index_removed = 2
changed_string = ''
for char in range(0, len(my_string)):
if(char != index_removed):
changed_string += my_string[char]
print("The string after removing ", index_removed, "nd character is : ")
print(changed_string) 출력
The string is : Hi there how are you The string after removing 2 nd character is : Hithere how are you
설명
-
문자열이 정의되고 콘솔에 표시됩니다.
-
인덱스 값이 정의됩니다.
-
문자열은 반복되며 문자열의 문자가 제거해야 하는 인덱스 값과 동일하지 않은 경우 해당 문자는 새 문자열에 배치됩니다.
-
이 새 문자열은 콘솔에 출력으로 표시됩니다.