문자열 입력

문자열 길이만큼 루프 실행


문자열 다루기
문자열 슬라이싱

문자열 합치기 - ‘+’ 연산자 사용

in 을 논리 연산자로 사용

문자열 라이브러리

Strip 메소드

시작 문자열 찾기

파일 열기 - open()

open('파일명입력', '모드 선택')
파일 읽기
fhand = open('Hamlet.txt')
for line in fhand :
print(line)
# 다음을 출력하게 되면 한줄씩 띄워져서 출력되게 됩니다.
fhand = open('Hamlet.txt')
count = 0
for line in fhand :
count = count + 1
print('Line Count: ', count)
# Line Count: 35로 출력됩니다.
fhand = open('mbox-short.txt')
inp = fhand.read()
print(len(inp))
# 94646으로 출력됩니다.
print(inp[:20])
# From stephen.marquar으로 출력됩니다.
fhand = open('mbox-short.txt')
for line in fhand:
line = line.rstrip() # 오른쪽 공백 제거
if line.startswith('From:') :
print(line)
# 결과값으로 From: 으로 시작되는 문자열이 출력되게 됩니다.
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened: ', fname)
quit()