*이스케이프 코드
\n : 줄 바꿈
\t : 문자열 사이 탭 간격 줌
\\ : 문자 \ 그대로 표현
\' : ' 그대로 표현
\" : " 그대로 표현
\r : 현재 커서 가장 앞 이동
\f : 현재 커서 다음 줄 이동
\a : 출력 시 '삑' 소리 남
\b : 백스페이스
\000 : null 문자
1. 문자열 종류
" " : 큰따옴표
' ' : 작은따옴표
""" """ : 큰따옴표 3개
''' ''' : 작은따옴표 3개
2.(') 문자열에 작은따옴표 포함
Python's favortie food is perl
food="Python's favortie food is perl"
print(food)
Python's favortie food is perl
문자열에 큰따옴표(") 포함
"Python is very easy." he says.
say='"Python is very easy." he says.'
print(say)
"Python is very easy." he says.
백슬래시(\) 사용 -> (') + (") 문자열 포함
Python's is favorite food is perl
food='Python\'s favorite food is perl'
print(food)
#\뒤 ' 혹은 "는 문자 그 자체 뜻함
Python's favortie food is perl
"Pythonn is very easy." he says.
say="\"Python is very easy.\" he says."
print(say)
#\뒤 ' 혹은 " = just 문자 그 자체
"Python is very easy." he says.
줄 바꿈 '\n'
Life is too short
You need python
multiline="Life is too short\nYou need python"
print(multiline)
"""
\n : 줄 바꿈 이스케이프 코드
"""
Life is too short
You need python
연속된 ''' / """ 사용
multiline='''
Life is too short
You need python
'''
#\n 대신 ''' 사용
print(multiline)
Life is too short
You need python
multiline="""
Life is too short
You need python
"""
#\n 대신 """ 사용
print(multiline)
Life is too short
You need python
3. 문자열 연산
Python + is fun!
head="Python"
tail=" is fun!"
print(head+tail)
Python is fun!
Python * 2
a="Python"
print(a*2)
#문자열 두 번 반복
PythonPython
= * 50
print("="*50)
print("My Program")
print("="*50)
==================================================
My Program
==================================================
문자열 길이
[error]
a="Life is too short" 변수명 지정하고
문자열 길이 구하는 함수인 len() 을 사용하여
len(a) 입력했으나, 터미널 내 결과가 나오지 않았다.
IDLE에서는 결과가 나왔으나 visual studio code에서는 결과가 나오지 않았다.
혹시나 하는 마음에 print를 입력했는데 결과가 나왔다.
...ㅋ
a="Life is too short"
print(len(a))
#print 내 함수 사용
17
문자열 인덱싱
: Life = 0123
: L=0
a="Life is too short, You need Python"
print(a[3])
#python은 0부터 숫사 셈
e
:n번째 글자
a="Life is too short, You need Python"
print(a[0])
L
:뒤에서 -n번째 글자
a="Life is too short, You need Python"
print(a[-2])
#뒤에서 2번째 글자 출력
o
문자열 슬라이싱
: 단어 추출
: 공백도 문자에 포함
a="Life is too short, You need Python"
b=a[0]+a[1]+a[2]+a[3]
print(b)
Life
a="Life is too short, You need Python"
print(a[0:4])
Life
a[시작 번호:끝 번호]
: 시작 번호부터 끝 번호까지 추출
: 끝 번호 부분 생략 -> 시작 번호부터 마지막 문자열까지 추출
a="Life is too short, You need Python"
print(a[19:])
#19번 글자부터 끝까지 추출
You need Python
a="Life is too short, You need Python"
print(a[:17])
#문자열 처음부터 17번째 번호까지 추출
Life is too short
a="Life is too short, You need Python"
print(a[:])
#처음부터 끝까지 추출
Life is too short, You need Python
a="Life is too short, You need Python"
print(a[19:-7])
#19번 문자열부터 뒤에서 7번째 문자열까지
You need
a="20010331Rainy"
date=a[:8]
weather=a[8:]
print(date)
print(weather)
#date : 처음부터 8번째 글자까지 출력
#weather : 8번째부터 마지막 글자까지 출력
20010331
Rainy
a="20010331Rainy"
year=a[:4]
#0부터 숫자 매김
day=a[4:8]
weather=a[8:]
print(year)
print(day)
print(weather)
2001
0331
Rainy
슬라이싱 기법 (=글자 바꾸기)
: 문자열 요솟값은 바꿀 수 없다.
a="Pithon"
print(a[:1]+'y'+a[2:])
#i를 제외한 모든 글자수+중간 y 더하기
Python
'[Language] > Python' 카테고리의 다른 글
[Jupyter Notebook] Index 제거 후 CSV 파일 저장 (0) | 2022.07.28 |
---|---|
Pip LightGBM : OSError (0) | 2022.07.26 |
문자열 입력(3) (0) | 2022.03.14 |
문자열 입력(2) (1) | 2022.03.07 |
연산자 (0) | 2022.03.06 |