본문으로 바로가기

2.자료형 - 문자

category IT/애플파이썬 2022. 5. 4. 00:09

이스케이프 코드란?

: 프로그래밍할 때 사용할 수 있도록 미리 정의해 둔 '문자조합'

코드 설명
\n 줄바꿈
\t
\\ \출력
\' 작은따옴표(') 출력
\" 큰따옴포(") 출력
\r 현재 커서 가장 앞으로 
\f 현재 커서 다음줄
\a 출력시 '삑'소리 나게함
\b 백스페이스
\000 널문자

 

1. 문자열 출력

food = "Python's favorite food is perl"
print(food)     #Python's favorite food is perl

# food = 'Python's favorite food is perl' -> 오류남

say = '"Python is very easy." he says.'
print(say)      #"Python is very easy." he says.

#백슬래시(\)를 사용해서 작은 따옴표(')와 큰 따옴표(")를 문자열에 포함시키기
food = 'Python\'s favorite food is perl'
say = "\"Python is very easy.\" he says."
print(food)     #Python's favorite food is perl
print(say)      #"Python is very easy." he says.

# 줄을 바꾸는 이스케이프 코드 '\n' 삽입
mutiline = "Life is too short\nYou need python"
print(mutiline)
#Life is too short
#You need python

mutiline = '''
... Life is too short
... You need python
... '''
print(mutiline)

 

2. 문자열 연산

#문자열 연산

#문자열 더하기
head = "Python"
tail = " is fun!!"
print(head + tail)  #Python is fun!!

#문자열 곱하기
a = "Python"
print(a*3)          #PythonPythonPython

#문자열 길이구하기
a = "bravo bravo"
print(len(a))       #11

'IT > 애플파이썬' 카테고리의 다른 글

1. 자료형- 숫자  (0) 2022.05.03