Python/Python

[Python] 문자열 | 문자열 index | 슬라이싱

기록하는_사람 2022. 9. 22. 20:55

문자열

📌 문자열에는 순서가 있음. 

      즉, index, indexing이라는 개념이 있음.

s = "hello world"

for i in range(0, len(s)):
    print(s[i])
    
# h
# e
# l
# l
# o

# w
# o
# r
# l
# d

 

문자열 index

📌 문자열은 음수 인덱스 사용 가능.

 

📌 음수 인덱스

: 음수로 되어있는 인덱스를 말하며, 마지막 인덱스가 -1로 시작함.

s = "Hello"

for i in range(0, len(s)):
    print(f"s[{i}] : {s[i]}")
print()

for i in range(-1, -(len(s)) - 1, -1):
    print(f"s[{i}] : {s[i]}")

# s[0] : H
# s[1] : e
# s[2] : l
# s[3] : l
# s[4] : o

# s[-1] : o
# s[-2] : l
# s[-3] : l
# s[-4] : e
# s[-5] : H

 

💡 index와 indexing

https://codingrecord2209.tistory.com/77

 

[Python] index와 indexing

Indexing(인덱싱) : index(인덱스)를 활용해 자료에 접근하는 것을 말함. 예) list[index], tuple[index] Index(인덱스) : 자료의 순서를 나타내는 것. 0부터 시작. * index(인덱스)가 0부터 시작하는 이유 : 최..

codingrecord2209.tistory.com

 

슬라이싱

📌 문자열은 슬라이싱 가능.

 

📌 슬라이싱은 자료를 나누는 것을 말함.

 

📌 자료 [start : stop : step]

: start부터 step만큼 건너뛰며 (stop - 1)까지를 의미하며, start, stop, step 모두 생략이 가능함.

s = "Hello Nice to meet you"

print(s[:5]) # Hello
print(s[6:]) # Nice to meet you
print(s[::2]) # HloNc ome o
print(s[6:10]) # Nice

 

 

💡 슬라이싱

https://codingrecord2209.tistory.com/78

 

[Python] 슬라이싱

슬라이싱 : 자료를 나눌 수 있는 것. 인덱스가 있는 자료형 모두 슬라이싱 가능. - 자료 [start : stop : step] : start 부터 (stop - 1)까지. - start, stop, step 모두 생략 가능. s = '19951004' s[:4] # start..

codingrecord2209.tistory.com

 

문자열 메서드

https://codingrecord2209.tistory.com/80

 

[Python] 문자열 함수

문자열 함수 - .split( ) : 문자열.split(문자) 문자열을 입력한 '문자' 기준으로 자르는 함수. 리스트로 반환함. s = ("Hi-Nice-to-meet-you") print(s.split("-")) # ['Hi', 'Nice', 'to', 'meet', 'you'] - .sp..

codingrecord2209.tistory.com