Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Algorithm
- javascript
- Class
- Pipelining
- react
- while
- XML
- DS
- MacOS
- function
- web
- CSS
- Linux
- DB
- python
- instruction
- DATAPATH
- data structure
- Java
- mysql
- html
- control
- MIPS
- for
- architecture
- computer
- php
- system
- github
- DoM
Archives
- Today
- Total
YYYEJI
[Python] 문자열 인덱싱 및 슬라이싱 본문
728x90
문자열 인덱싱이란?
무언가(특정 위치)를 가르키는 것입니다.
string이라는 변수 안에 Hello라는 문자열이 있습니다.
string = "Hello"
문자열에 번호를 매겨보겠습니다.
H | e | l | l | o |
0 | 1 | 2 | 3 | 4 |
-5 | -4 | -3 | -2 | -1 |
컴퓨터에서는 문자열의 위치가 0부터 시작을 합니다.
string을 인덱싱 하면 ?
len(string) # 5 # length
string[0] # H
string[1] # e
stirng[-1] # o
string을 슬라이싱 하면 ?
print(string[0:5]) # Hello
print(string[1:]) # ello
print(string[:5]) # Hello
print(string[2:3]) # l
print(string[:]) # Hello
variable_name[start index:last index -1]
이기 때문에 :(colon) 뒤에는 +1 값을 넣어줘야 됩니다.
◡̈
'Python' 카테고리의 다른 글
[Python] 문자열을 합치고 나누는 함수 (split, join) (0) | 2022.09.24 |
---|---|
[Python] In, not in 포함 연산자 (0) | 2022.09.24 |
[Python] 대소문자 구분없이 정렬하는 sort 함수 (0) | 2022.09.23 |
[Python] 리스트(list) 정리 (0) | 2022.09.23 |
[Python] 이스케이프(Escape) 문자란? (0) | 2022.09.23 |