반응형
String
문자열
"~~"
'~~'
"""~~"""
- input()을 이용하면 string으로 저장되었었음
1. Raw String: 특수 부호
\n (next line)
\t (tab만큼 이동)
'
"
r'o': 불가침 영역으로 인정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | path = "c:\temp" print(path) # c: emp # \를 이용 print("c:\\temp") # c:\temp # \ escape sequence path = 'c:\\program File\\python\\bin' print(path) # c:\program File\python\bin # r''이용 print(r'c:\program File\python\bin') #c:\program File\python\bin #Let's go # print(r'Let's go') # SyntaxError: invalid syntax print("Let's go") # Let's go print('"Here", she said') # "Here", she said | cs |
2. str()과 repr()
1 2 3 4 5 6 7 | hello = 'Hello, world\n' hello2 = repr(hello) print(hello) # Hello, world # (\n) print(hello2) # 'Hello, world\n' #다음줄로 넘어가지 말고, \n을 출력시켜라! | cs |
3. Formatted printing
print()에서 여러가지 사용이 가능하다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | for x in range(1,11): print(repr(x).rjust(2), repr(x*x).rjust(3), end='') print(repr(x*x*x).rjust(4)) # 1 1 1 # 2 4 8 # 3 9 27 # 4 16 64 # 5 25 125 # 6 36 216 # 7 49 343 # 8 64 512 # 9 81 729 # 10 1001000 | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | for x in range(1,11): print(repr(x).rjust(2), repr(x*x).rjust(3), repr(x*x*x).rjust(4)) # 1 1 1 # 2 4 8 # 3 9 27 # 4 16 64 # 5 25 125 # 6 36 216 # 7 49 343 # 8 64 512 # 9 81 729 # 10 100 1000 | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | for x in range(1, 11): print("\t",x, end='') # 1 2 3 4 5 6 7 8 9 10 for x in range(1, 11): print("\t", x) # 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10 | cs |
4. String method
1) find
- 있으면 index값
- 없으면 -1 출력
1 2 3 4 5 | # find title = "Monty Python's Flying Circus" print(title.find("Monty")) #0 print(title.find("Python")) #6 print(title.find("DB")) #-1 | cs |
find('word', start, end)
1 2 3 4 5 6 | subject = '$$$ Get rich now !!! $$$' print(subject.find('$$$')) # 0 # find('word', start, end) print(subject.find('$$$', 3)) # 21 print(subject.find('!!!', 0, 5)) # -1 | cs |
2) join
- split -> join
- list로 분해되었던 아이들을 합칠때 이용함
*주의* 문자열에서만 사용가능하다.
1 2 3 4 5 6 7 8 | # seq = [1, 2, 3, 4, 5] # sep = '+' # sep.join(seq) # TypeError: sequence item 0: expected str instance, int found seq = ['1', '2', '3', '4', '5'] print('+'.join(seq)) # 1+2+3+4+5 sep = '+' print(sep.join(seq)) # 1+2+3+4+5 | cs |
window 경로지정할때 유용함 ㅋㅋㅋㅋㅋ
1 2 3 4 | dirs = '', 'usr', 'bin', 'env' print(dirs) # ('', 'usr', 'bin', 'env') print('/'.join(dirs)) # /usr/bin/env print('c:'+'\\'.join(dirs)) # c:\usr\bin\env | cs |
3) lower()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | sen = "Think like a man of action and act like man of thought." if 'think' in sen: print("sentence:",sen,'\nthink in sentence!') else: print("sentence:",sen,'\nthink not in sentence!') # sentence: Think like a man of action and act like man of thought. # think not in sentence! sen = "Think like a man of action and act like man of thought." if 'think' in sen.lower(): print("sentence:",sen,'\nthink in sentence!') # sentence: Think like a man of action and act like man of thought. # think in sentence! else: print("sentence:",sen,'\nthink not in sentence!') | cs |
4) replace('before', 'after')
1 2 3 | # replace sen = 'This is a test' print(sen.replace('is', 'was')) | cs |
5) split(default=' ')
1 2 3 4 5 6 7 | # split (default =' ') sen = 'This is a sentence' print(sen.split(' ')) # ['This', 'is', 'a', 'sentence'] print(sen.split()) # ['This', 'is', 'a', 'sentence'] sen = '/usr/bin/env' print(sen.split('/')) # ['', 'usr', 'bin', 'env'] | cs |
6) strip(default=' ')
양 끝의 공백 제거할때 사용
1 2 3 4 5 | # strip() sen = ' ***internal white space is not be***' print(sen.strip()) #***internal white space is not be*** print(sen.strip(' ')) #***internal white space is not be*** print(sen.strip('*')) # ***internal white space is not be | cs |
반응형
'Data > python·알고리즘' 카테고리의 다른 글
[python 기초] 조건문과 Loop문 + List Comprehension (0) | 2018.06.28 |
---|---|
[python 기초] python의 dictionary에 대하여 (0) | 2018.06.28 |
[python 기초] python의 tuple에 대하여 (0) | 2018.06.27 |
[python 기초] python의 list에 대하여 (0) | 2018.06.27 |
[python 기초] 데이터 타입과 변수 (0) | 2018.06.26 |
댓글