Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python_study
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Minseong Kwon
python_study
Commits
a53882db
Commit
a53882db
authored
6 years ago
by
Minseong Kwon
Browse files
Options
Downloads
Patches
Plain Diff
Update 문자열 인덱싱과 슬라이싱.txt 주석달기
parent
6156bd22
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
문자열 인덱싱과 슬라이싱.txt
+19
-9
19 additions, 9 deletions
문자열 인덱싱과 슬라이싱.txt
with
19 additions
and
9 deletions
문자열 인덱싱과 슬라이싱.txt
+
19
−
9
View file @
a53882db
...
...
@@ -3,12 +3,12 @@ Type "help", "copyright", "credits" or "license()" for more information.
>>> #문자열 인덱싱과 슬라이싱
>>>
>>> a="life is too short"
>>> a[0]
>>> a[0]
#a[번호]는 문자열 내 특정 값을 뽑아내는 역할을 하고, 이런한 것을 인덱싱 이라한다.
'l'
>>> a[3]
'e'
>>> a[2]
'f'
'f'
#파이썬은 0부터 숫자를 센다. a[3] 은 네번째 문자를 뜻한다.
>>>
>>> a[9]
'o'
...
...
@@ -26,14 +26,14 @@ Type "help", "copyright", "credits" or "license()" for more information.
'r'
>>> a[16]
't'
>>> a[17]
>>> a[17]
# 문자열 길이가 17이므로 인덱스는 16까지만 존재
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
a[17]
IndexError: string index out of range
>>> a[16]
't'
>>> len(a)
>>> len(a)
#순서열 길이를 재는 함수 len, 문자열에서도 사용 가능하다.
17
>>> a[-1]
't'
...
...
@@ -41,15 +41,19 @@ IndexError: string index out of range
'r'
>>> a[-0]
'l'
>>> b=a[0]+a[1]+a[2]+a[3]
>>> b=a[0]+a[1]+a[2]+a[3]
#문자열에서 단어를 뽑아내는 것이 아닌 다음과 같이 단어를 뽑아낼 수도 있다.
>>> b
'life'
>>> a[0:4]
>>> a[0:4]
# 위의 방법처럼 할 수 도 있지만, 파이썬에서는 슬라이싱이라는 기법을 제공한다.
'life'
>>> a[0:3]
'lif'
>>> a[0:5]
'life '
'life ' #a[시작번호, 끝번호] 지정하면 끝번호에 해당하는 것은 포함이 되지 않는다.
#a[0:5]는 a[0]+a[1]+a[2]+a[3]+a[4] 와 동일하다.
#이때 a[4]는 공백이기 때문에 'life' 가 아닌 'life ' 가 출력된다.
>>> a[0:]
'life is too short'
>>> a[:17]
...
...
@@ -61,7 +65,8 @@ IndexError: string index out of range
>>> a[:100]
'life is too short'
>>> a[1:18]
'ife is too short'
'ife is too short' #a[시작번호:끝번호] 에서 시작번호를 생략하면 문자열의 처음부터 끝번호 뽑아낸다.
#끝번호를 생략하면 시작부터 끝까지를 뽑아냄
>>> a[1:100]
'ife is too short'
>>> a[18]
...
...
@@ -77,7 +82,12 @@ IndexError: string index out of range
>>> a[16]
't'
>>> a[2:-2]
'fe is too sho'
'fe is too sho' #-기호는 뒤에서부터 읽이위해 기호를 붙이는 것이다
#a[-2]는 뒤에서 두번째 문자 a[-1]은 뒤에서 첫번째 문자이다.
#a[5,-2]는 a[5]부터 a[-3]까지 뽑아낸다
>>> a[5:-2]
'is too sho'
>>> a[-1]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment