반응형

본 프로그램은 python 2.7.13에서 제작되었습니다. 



Str 전체 메소드에 대한 설명을 하고자 한다.


이 글을 전체 다 봐도 되고, ctrl + f를 통해 메소드를 찾아 써도 된다.


# str.capitalize()

# str의 첫번째 문자를 대문자로 만들어준다.


a = 'hello world'

b = '123hello world'


print ''

print 'str.capitalize()'

print a.capitalize()

print b.capitalize()


# str.center(width[, fillchar])

# width - str의 크기 > 0일 경우 가운데 정렬을 시작해준다.

# 이때 fillchar은 선택사항이고 공백 대신 채워줄 문자열을 의미한다.


a = 'center string'


print ''

print 'str.center(width[, fillchar])'

print a.center(13, '#')

print a.center(14, '#')

print a.center(15, '#')


# str.count(sub[, start[, end]])

# str내에 sub 문자열이 몇개 있는지 조사해준다.

# start, end는 선택사항이고 시작, 끝 인덱스를 말해준다.


a = 'count start count end'


print ''

print 'str.count(sub, [,start[, end]])'

print a.count('count')

print a.count('count', 1)

print a.count('count', 0, 15)


# str.decode와 str.encode는 여기 게시물에서는 생략합니다.


# str.endswith(suffix[, start[, end]])

# str의 끝부분이 suffix 문자열과 일치하는지 확인한다.

# 맞으면 True, 틀리면 False를 반환

# 이때 start, end는 선택사항이고 시작, 끝 인덱스를 지정 가능하다.


a = 'aaabbbccc'

print ''

print 'str.endswith(suffix[, start[, end]])'

print a.endswith('c')

print a.endswith('ccc')

print a.endswith('cccc')


# str.expandtabs([tabsize])

# 탭으로 표현된 부분을 공백 몇칸으로 재설정 해줄지 정해준다.

# 이때 default는 8이다.


a = 'a\tb\tc'


print ''

print 'str.expandtabs([tabsize])'

print a.expandtabs()

print a.expandtabs(5)

print a.expandtabs(10)


# str.find(sub [,start [,end]])

# str 내의 sub가 어디에 위치하는지 가장 첫번째 인덱스를 알려준다.

# 만약 찾기가 실패한다면 -1을 리턴해준다.

# start, end는 선택사항이고 시작, 끝 인덱스를 설정 할 수 있다.


a = 'my name is crocus'


print ''

print 'str.find(sub [,start [,end]])'

print a.find('crocus')

print a.find('program')

print a.find('crocus', 12)


# str.format(*args, **kwargs)

# 아래 예제를 통해 확인하는 것이 더 쉽다.


a = 'python {0} is lower version than python {1}'


print ''

print 'str.format(*args, **kwargs)'

print a

print a.format(2,3)


a = 'python {first} is lower version than python {second}'


print a

print a.format(first = 'two', second = 'three')



# str.isalnum()

# str 문자열이 숫자와 영어로 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = '123abc'


print ''

print 'str.isalnum()'

print a.isalnum()


a = '!@12ab'

print a.isalnum()


# str.isalpha()

# str 문자열이 영어로만 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = 'alphabet'


print ''

print 'str.isalpha()'

print a.isalpha()


a = 'one 1 two 2 three 3'

print a.isalpha()


# str.isdigit()

# str 문자열이 숫자로만 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = '12345'


print ''

print 'str.isdigit()'

print a.isdigit()


a = '1.123' # 소수점 때문에 isdigit에 해당하지 않는다.


print a.isdigit()


a = 'one 1 two 2 three 3'


print a.isdigit()


# str.islower()

# str 문자열이 모두 소문자로만 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = 'abcd'


print ''

print 'str.islower()'

print a.islower()


a = 'ABcd'


print a.islower()


# str.isspace()

# str 문자열이 모두 공백으로만 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = '\t\n   '

b = '123'


print ''

print 'str.isspace()'

print a.isspace() # 탭, 개행, 공백 모두 isspace에서 True


a = 'gg  gl'

print a.isspace()


# str.istitle()

# 각 단어의 첫번째 문자가 대문자이고 그다음 문자가 모두 소문자인지 확인한다.

# 영문자에 대해서만 판단하고 그 외 문자는 상관없다

# 맞으면 True, 틀리면 False를 반환


a = 'I Am Crocus1!'


print ''

print 'str.istitle()'

print a.istitle()


a = 'i Am crocus'


print a.istitle()


a = 'I Am CRocus'


print a.istitle()


a = '1.First Of All'


print a.istitle()


# str.isupper()

# str 문자열이 모두 대문자로만 이루어진지 확인한다.

# 맞으면 True, 틀리면 False를 반환


a = 'ABCD'


print ''

print 'str.isupper()'

print a.isupper()


a = 'ABcd'


print a.isupper()



# str.join(iterable)

# iterable 한 것에 대해 구분자를 만들어주는 역할을 한다.


a = 'helloworld'


print ''

print 'str.join()'


b = ','.join(a)

print b

b = '--'.join(a)

print b


# str.ljust(width[, fillchar])

# width - str > 0일때 width - str 크기만큼 오른쪽에서 들여쓰기를 해준다.

# 즉, 왼쪽 정렬과 같다.


a = 'hello world'


print ''

print 'str.ljust(width[, fillchar])'

print a.ljust(11, '*')

print a.ljust(12, '*')

print a.ljust(20, '*')


# str.lower()

# 대문자를 소문자로 변환해준다.


a = 'HELlO wORLD'


print ''

print 'str.lower()'

print a.lower()


# str.lstrip([chars])

# chars에 해당하는 모든 조합이 가장 왼쪽에 존재하면 지워준다.


a = 'ddddaaacc bb'


print ''

print 'str.lstrip([chars])'

print a.lstrip('d')

print a.lstrip('dd')

print a.lstrip('ddd')


# str.partiton(sep)

# sep를 기준으로 3-tuple을 리턴해준다.

# 이때 기준이 되는 sep가 없다면 empty로 튜플에 리턴

# 그리고 sep와 매칭되는 가장 첫번째 것을 기준으로 partition


a = '123.456'

b = 'ab111dc'


print 'str.partition(sep)'

print a.partition('.')

print a.partition('0')


print b.partition('111')

print b.partition('1')


# str.replace(old, new[, count])

# 기존 str에 있는 문자열을 new 문자열로 바꿔준다.

# 이때 count는 선택이고, count의 의미는 앞에서 몇개까지 바꿀지 결정해준다.


a = 'hello world'


print ''

print 'str.replace(old, new[, count])'

print a.replace('l','#')

print a.replace('l', '#', 2)



# str.rfind(sub [,start [,end]])

# str의 오른쪽에서 부터 sub를 찾아준다.

# 이때 start와 end는 선택이고, 각 시작, 끝 인덱스를 설정 할 수 있다.


a = 'hello hello'


print ''

print 'str.rfind(sub [,start [, end]])'

print a.rfind('hello')

print a.rfind('hello', 6, 10) # 이때는 6 <= idx < 10이기에 마지막 hell만 볼 수 있기에 -1 리턴

print a.rfind('hello', 6, 11) # 이때는 6 <= idx <= 10이기에 마지막 hello가 있는 6을 리턴



# str.rindex(sub [,start [,end]])

# str의 오른쪽부터 시작하여 sub를 찾아준다.

# 이때 start와 end는 선택이고, 각 시작, 끝 인덱스를 설정할 수 있다.

# index는 find와 다르게 찾지 못하면 -1 리턴이 아닌 에러를 발생한다.


a = 'hello hello'


print ''

print 'str.rindex(sub [,start [, end]])'

print a.rindex('hello')

#print a.rindex('hello', 6, 10) # index 메서드 자체가 원하는 인덱스를 찾지 못한 경우 -1이 아닌 에러를 리턴하게 된다.

print a.rindex('hello', 6, 11) # index 메서드에서 원하는 인덱스를 찾았기에 6을 리턴


# str.rjust(width[, fillchar])

# width - str > 0일때 width - str 크기만큼 왼쪽에서 들여쓰기를 해준다.

# 즉, 오른쪽 정렬과 같고 c에서 %10d같은 내용과 비슷


a = 'hello world'


print ''

print 'str.rjust(width[, fillchar])'

print a.rjust(11, '*')

print a.rjust(12, '*')

print a.rjust(20, '*')


# str.rpartition(sep)

# 위의 partition과 다른 점은 partition은 왼쪽에서 처음 나오는 sep을 기준으로 partition

# rpartition은 오른쪽에서 처음 나오는 sep을 기준으로 partition


a = 'hello world bye'


print ''

print 'str.rpartition(sep)'

print a.rpartition(' ')



# str.rsplit(sep [,maxsplit]])

# sep을 기준으로 오른쪽부터 split해준다. maxsplit는 선택이고 default 값은 모든 것을 다 split해준다.

# 그리고 maxsplit에 인자를 n개 넣어준다면 n개의 ,으로 split해준다.


a = 'hi hello bye bye'


print ''

print 'str.rsplit(sep [,maxsplit]])'

print a.rsplit(' ')

print a.rsplit(' ',1)

print a.rsplit(' ',2)

print a.rsplit(' ',3)


# str.rstrip([chars])

# chars에 해당하는 모든 조합이 가장 오른쪽에 존재하면 지워준다.


a = 'hello worldddd'


print ''

print 'str.rstrip([chars])'

print a.rstrip('d')

print a.rstrip('dd') 

print a.rstrip('ddd') # ddd의 조합 중 d가 포함되기에 오른쪽 d 4개가 지워진다. 


# str.split([sep [,maxsplit]])

# 왼쪽부터 sep을 기준으로 split해준다.

# 이때 maxsplit는 옵션이고, 최대 몇개를 split할지 결정해준다.

# maxsplit를 입력하지 않으면 default로는 str내의 sep을 기준으로 모두 split한다.


a = 'split#world#show'


print ''

print 'str.slplit([sep [,maxsplit]])'

print a.split('#')

print a.split('#',1)

print a.split('#',2)


# str.splitlines(keepends=False)


a = 'python\nis\ngood'


print ''

print 'str.splitlines(keepends=False)'

print a

print a.splitlines()

print a.splitlines(True) # 구분자를 제거

print a.splitlines(False) # 구분자를 제거하지 않는다.

b = a.splitlines(True)

c = a.splitlines(False)

print b

print c


# str.startswith(prefix[, start[, end]])

# 앞의 시작 문자열이 prefix와 동일한지 확인한다.

# 이때 start, end는 선택사항이고 시작 인덱스 끝 인덱스를 지정해 줄 수 있다.

# 맞으면 True, 틀리면 False를 반환한다.


a = 'startswith find'


print ''

print 'str.startswith(prefix[, start[, end]])'

print a.startswith('st')

print a.startswith('st',1)

print a.startswith('st',0, 2)


# str.strip([chars])

# str 문자열의 앞 뒤 공백을 지워주는 역할을 한다.

# 이때 chars 값이 있다면 공백이 아닌 그 값을 지워준다.


a = '  haha haha  '


print ''

print 'str.strip([chars])'

print a.strip()


a = 'haha haha'

print a.strip('h')

print a.strip('ha') # 공백을 제외하고 ha가 모두 지워진다.



# str.swapcase()

# str 문자열에서 대문자는 소문자로, 소문자는 대문자로 바꿔준다.


a = 'helLo WoRLD 123 !!@'


print ''

print 'str.swapcase()'

print a.swapcase()


# str.title()

# str 문자열 내의 모든 단어의 가장 처음 영문자를 대문자로 바꿔준다.


a = '1.first of all'


print ''

print 'str.title()'

print a.title()



# str.translate(table, [,deletechars])

# translate를 쓰기 위해 테이블 형성이 필요한데 이때 from string import maketrans를 해준다.

# 우선 a,b를 1:1매칭으로 테이블화 시킨다.

# 그리고 아래처럼 이용할 수 있다.


a = 'abcd'

b = '1234'

from string import maketrans

c = maketrans(a,b)


print ''

print 'str.translate(table, [,deletechars])'

print type(c)

print c

print a.translate(c)


# str.upper()

# str의 소문자를 대문자로 바꾼다.


a = 'heLLo worlD'


print ''

print 'str.upper()'

print a.upper()


# str.zfill(width)

# width - str 크기 > 0 일경우 width - str 크기만큼 0을 왼쪽에 넣어준다.


a = 'hello world'

print a.zfill(11)

print a.zfill(12)

print a.zfill(20)















소스 코드 : 


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# str.capitalize()
# str의 첫번째 문자를 대문자로 만들어준다.
 
= 'hello world'
= '123hello world'
 
print ''
print 'str.capitalize()'
print a.capitalize()
print b.capitalize()
 
# str.center(width[, fillchar])
# width - str의 크기 > 0일 경우 가운데 정렬을 시작해준다.
# 이때 fillchar은 선택사항이고 공백 대신 채워줄 문자열을 의미한다.
 
= 'center string'
 
print ''
print 'str.center(width[, fillchar])'
print a.center(13'#')
print a.center(14'#')
print a.center(15'#')
 
# str.count(sub[, start[, end]])
# str내에 sub 문자열이 몇개 있는지 조사해준다.
# start, end는 선택사항이고 시작, 끝 인덱스를 말해준다.
 
= 'count start count end'
 
print ''
print 'str.count(sub, [,start[, end]])'
print a.count('count')
print a.count('count'1)
print a.count('count'015)
 
# str.decode와 str.encode는 여기 게시물에서는 생략합니다.
 
# str.endswith(suffix[, start[, end]])
# str의 끝부분이 suffix 문자열과 일치하는지 확인한다.
# 맞으면 True, 틀리면 False를 반환
# 이때 start, end는 선택사항이고 시작, 끝 인덱스를 지정 가능하다.
 
= 'aaabbbccc'
print ''
print 'str.endswith(suffix[, start[, end]])'
print a.endswith('c')
print a.endswith('ccc')
print a.endswith('cccc')
 
# str.expandtabs([tabsize])
# 탭으로 표현된 부분을 공백 몇칸으로 재설정 해줄지 정해준다.
# 이때 default는 8이다.
 
= 'a\tb\tc'
 
print ''
print 'str.expandtabs([tabsize])'
print a.expandtabs()
print a.expandtabs(5)
print a.expandtabs(10)
 
# str.find(sub [,start [,end]])
# str 내의 sub가 어디에 위치하는지 가장 첫번째 인덱스를 알려준다.
# 만약 찾기가 실패한다면 -1을 리턴해준다.
# start, end는 선택사항이고 시작, 끝 인덱스를 설정 할 수 있다.
 
= 'my name is crocus'
 
print ''
print 'str.find(sub [,start [,end]])'
print a.find('crocus')
print a.find('program')
print a.find('crocus'12)
 
# str.format(*args, **kwargs)
# 아래 예제를 통해 확인하는 것이 더 쉽다.
 
= 'python {0} is lower version than python {1}'
 
print ''
print 'str.format(*args, **kwargs)'
print a
print a.format(2,3)
 
= 'python {first} is lower version than python {second}'
 
print a
print a.format(first = 'two', second = 'three')
 
 
# str.isalnum()
# str 문자열이 숫자와 영어로 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= '123abc'
 
print ''
print 'str.isalnum()'
print a.isalnum()
 
= '!@12ab'
print a.isalnum()
 
# str.isalpha()
# str 문자열이 영어로만 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= 'alphabet'
 
print ''
print 'str.isalpha()'
print a.isalpha()
 
= 'one 1 two 2 three 3'
print a.isalpha()
 
# str.isdigit()
# str 문자열이 숫자로만 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= '12345'
 
print ''
print 'str.isdigit()'
print a.isdigit()
 
= '1.123' # 소수점 때문에 isdigit에 해당하지 않는다.
 
print a.isdigit()
 
= 'one 1 two 2 three 3'
 
print a.isdigit()
 
# str.islower()
# str 문자열이 모두 소문자로만 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= 'abcd'
 
print ''
print 'str.islower()'
print a.islower()
 
= 'ABcd'
 
print a.islower()
 
# str.isspace()
# str 문자열이 모두 공백으로만 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= '\t\n   '
= '123'
 
print ''
print 'str.isspace()'
print a.isspace() # 탭, 개행, 공백 모두 isspace에서 True
 
= 'gg  gl'
print a.isspace()
 
# str.istitle()
# 각 단어의 첫번째 문자가 대문자이고 그다음 문자가 모두 소문자인지 확인한다.
# 영문자에 대해서만 판단하고 그 외 문자는 상관없다
# 맞으면 True, 틀리면 False를 반환
 
= 'I Am Crocus1!'
 
print ''
print 'str.istitle()'
print a.istitle()
 
= 'i Am crocus'
 
print a.istitle()
 
= 'I Am CRocus'
 
print a.istitle()
 
= '1.First Of All'
 
print a.istitle()
 
# str.isupper()
# str 문자열이 모두 대문자로만 이루어진지 확인한다.
# 맞으면 True, 틀리면 False를 반환
 
= 'ABCD'
 
print ''
print 'str.isupper()'
print a.isupper()
 
= 'ABcd'
 
print a.isupper()
 
 
# str.join(iterable)
# iterable 한 것에 대해 구분자를 만들어주는 역할을 한다.
 
= 'helloworld'
 
print ''
print 'str.join()'
 
= ','.join(a)
print b
= '--'.join(a)
print b
 
# str.ljust(width[, fillchar])
# width - str > 0일때 width - str 크기만큼 오른쪽에서 들여쓰기를 해준다.
# 즉, 왼쪽 정렬과 같고 c에서 
 
= 'hello world'
 
print ''
print 'str.ljust(width[, fillchar])'
print a.ljust(11'*')
print a.ljust(12'*')
print a.ljust(20'*')
 
# str.lower()
# 대문자를 소문자로 변환해준다.
 
= 'HELlO wORLD'
 
print ''
print 'str.lower()'
print a.lower()
 
# str.lstrip([chars])
# chars에 해당하는 모든 조합이 가장 왼쪽에 존재하면 지워준다.
 
= 'ddddaaacc bb'
 
print ''
print 'str.lstrip([chars])'
print a.lstrip('d')
print a.lstrip('dd')
print a.lstrip('ddd')
 
# str.partiton(sep)
# sep를 기준으로 3-tuple을 리턴해준다.
# 이때 기준이 되는 sep가 없다면 empty로 튜플에 리턴
# 그리고 sep와 매칭되는 가장 첫번째 것을 기준으로 partition
 
= '123.456'
= 'ab111dc'
 
print 'str.partition(sep)'
print a.partition('.')
print a.partition('0')
 
print b.partition('111')
print b.partition('1')
 
# str.replace(old, new[, count])
# 기존 str에 있는 문자열을 new 문자열로 바꿔준다.
# 이때 count는 선택이고, count의 의미는 앞에서 몇개까지 바꿀지 결정해준다.
 
= 'hello world'
 
print ''
print 'str.replace(old, new[, count])'
print a.replace('l','#')
print a.replace('l''#'2)
 
 
# str.rfind(sub [,start [,end]])
# str의 오른쪽에서 부터 sub를 찾아준다.
# 이때 start와 end는 선택이고, 각 시작, 끝 인덱스를 설정 할 수 있다.
= 'hello hello'
 
print ''
print 'str.rfind(sub [,start [, end]])'
print a.rfind('hello')
print a.rfind('hello'610# 이때는 6 <= idx < 10이기에 마지막 hell만 볼 수 있기에 -1 리턴
print a.rfind('hello'611# 이때는 6 <= idx <= 10이기에 마지막 hello가 있는 6을 리턴
 
 
# str.rindex(sub [,start [,end]])
 
= 'hello hello'
 
print ''
print 'str.rindex(sub [,start [, end]])'
print a.rindex('hello')
#print a.rindex('hello', 6, 10) # index 메서드 자체가 원하는 인덱스를 찾지 못한 경우 -1이 아닌 에러를 리턴하게 된다.
print a.rindex('hello'611# index 메서드에서 원하는 인덱스를 찾았기에 6을 리턴
 
# str.rjust(width[, fillchar])
# width - str > 0일때 width - str 크기만큼 왼쪽에서 들여쓰기를 해준다.
# 즉, 오른쪽 정렬과 같고 c에서 %10d같은 내용과 비슷
 
= 'hello world'
 
print ''
print 'str.rjust(width[, fillchar])'
print a.rjust(11'*')
print a.rjust(12'*')
print a.rjust(20'*')
 
# str.rpartition(sep)
# 위의 partition과 다른 점은 partition은 왼쪽에서 처음 나오는 sep을 기준으로 partition
# rpartition은 오른쪽에서 처음 나오는 sep을 기준으로 partition
 
= 'hello world bye'
 
print ''
print 'str.rpartition(sep)'
print a.rpartition(' ')
 
 
# str.rsplit(sep [,maxsplit]])
# sep을 기준으로 오른쪽부터 split해준다. 이때 maxsplit는 선택이고 default 값은 모든 것을 다 split해준다.
# 그리고 maxsplit에 인자를 n개 넣어준다면 n개의 ,으로 split해준다.
 
= 'hi hello bye bye'
 
print ''
print 'str.rsplit(sep [,maxsplit]])'
print a.rsplit(' ')
print a.rsplit(' ',1)
print a.rsplit(' ',2)
print a.rsplit(' ',3)
 
# str.rstrip([chars])
# chars에 해당하는 모든 조합이 가장 오른쪽에 존재하면 지워준다.
 
= 'hello worldddd'
 
print ''
print 'str.rstrip([chars])'
print a.rstrip('d')
print a.rstrip('dd'
print a.rstrip('ddd'# ddd의 조합 중 d가 포함되기에 오른쪽 d 4개가 지워진다. 
 
# str.split([sep [,maxsplit]])
# 왼쪽부터 sep을 기준으로 split해준다.
# 이때 maxsplit는 옵션이고, 최대 몇개를 split할지 결정해준다.
# maxsplit를 입력하지 않으면 default로는 str내의 sep을 기준으로 모두 split한다.
 
= 'split#world#show'
 
print ''
print 'str.slplit([sep [,maxsplit]])'
print a.split('#')
print a.split('#',1)
print a.split('#',2)
 
# str.splitlines(keepends=False)

 
= 'python\nis\ngood'
 
print ''
print 'str.splitlines(keepends=False)'
print a
print a.splitlines()
print a.splitlines(True) # 구분자를 제거
print a.splitlines(False) # 구분자를 제거하지 않는다.
= a.splitlines(True)
= a.splitlines(False)
print b
print c
 
# str.startswith(prefix[, start[, end]])
# 앞의 시작 문자열이 prefix와 동일한지 확인한다.
# 이때 start, end는 선택사항이고 시작 인덱스 끝 인덱스를 지정해 줄 수 있다.
# 맞으면 True, 틀리면 False를 반환한다.
 
= 'startswith find'
 
print ''
print 'str.startswith(prefix[, start[, end]])'
print a.startswith('st')
print a.startswith('st',1)
print a.startswith('st',02)
 
# str.strip([chars])
# str 문자열의 앞 뒤 공백을 지워주는 역할을 한다.
# 이때 chars 값이 있다면 공백이 아닌 그 값을 지워준다.
 
= '  haha haha  '
 
print ''
print 'str.strip([chars])'
print a.strip()
 
= 'haha haha'
print a.strip('h')
print a.strip('ha'# 공백을 제외하고 ha가 모두 지워진다.
 
 
# str.swapcase()
# str 문자열에서 대문자는 소문자로, 소문자는 대문자로 바꿔준다.
 
= 'helLo WoRLD 123 !!@'
 
print ''
print 'str.swapcase()'
print a.swapcase()
 
# str.title()
# str 문자열 내의 모든 단어의 가장 처음 영문자를 대문자로 바꿔준다.
 
= '1.first of all'
 
print ''
print 'str.title()'
print a.title()
 
 
# str.translate(table, [,deletechars])
# translate를 쓰기 위해 테이블 형성이 필요한데 이때 from string import maketrans를 해준다.
# 우선 a,b를 1:1매칭으로 테이블화 시킨다.
# 그리고 아래처럼 이용할 수 있다.
 
= 'abcd'
= '1234'
from string import maketrans
= maketrans(a,b)
 
print ''
print 'str.translate(table, [,deletechars])'
print type(c)
print c
print a.translate(c)
 
# str.upper()
# str의 소문자를 대문자로 바꾼다.
 
= 'heLLo worlD'
 
print ''
print 'str.upper()'
print a.upper()
 
# str.zfill(width)
# width - str 크기 > 0 일경우 width - str 크기만큼 0을 왼쪽에 넣어준다.
 
= 'hello world'
print a.zfill(11)
print a.zfill(12)
print a.zfill(20)
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형