본문 바로가기

Python

- 문법, 데이터타입, 숫자

어떠한 값을 만들어 내는 간단한 코드를 표현식(expression)이라고 함. - 값은 숫자, 수식, 문자열 등과 같은 것

표현식이 하나 이상 모이면 문장(statement), 파이썬은 한 줄이 하나의 문장.

문장이 모여서 프로그램(program)

 

출처 - https://docs.python.org/3/library/stdtypes.html

 

문자열의 표현

문자열 시작과 끝은 ' ' 또는 " "

기본적으로 작은 따옴표 많이 씀. 문자열 안에 작은 따옴표를 표현하고 싶을 때 큰따옴표를 사용.

큰따옴표도 사용하고 싶을 때, 큰따옴표 앞에 \를 붙이면 직후의 문자 하나를 기본 문자로 사용. (#escape)

#escape
print("hell'o' \"w\"orld")
 
 
새로운 줄을 사용하고 싶을 때.
\n
#newline
print('H\ne\nl\nl\no')

글 바꾸는 다른 방법.

문자열 맨 앞뒤 따옴표를 세개. 

#docstring
print('''
H
E
L
L
O
''')

 

문자열의 처리

변수  a=' '

문자가 몇 개인지 찾기

string length

a='hello pyhthon'
print(a)
#length
print(len(a))

 

string slice

S a m m y     S h a r k !

0 1 2  3  4 5 6 7 8 9 10 11

#index
print(a[0])
print(a[2:5])
#repeat
print((a+'\n')*2)

 

문자열과 변수

문자에서 특정한 부분만 바꾸기

name='Yoo'
print("Russia's invasion of Ukraine puts tech giants in an untenable situation.'+name+', the CEO of high-end electric vehicle manufacturer Tesla , and incidentally the richest man in the world, finds himself in a delicate position. by '+name+'.)

바꿀 텍스트가 1억번 반복한다면, 용량이 매우 거대해질 것. 변수를 사용한다면 데이터가 현저히 줄어들 것. 데이터 성격 파악이 좋아지므로 가독성이 좋아짐. 유지보수 편리해짐.

 

포맷팅

string format

출처 - https://pyformat.info

#positional formating
print("Russia's invasion of Ukraine puts tech giants in an untenable situation. {}({}), the CEO of high-end electric vehicle manufacturer Tesla , and incidentally the richest man in the world, finds himself in a delicate position. by {}.".format('Yoo',35,'Yoo'))

#Named placeholder
print("Russia's invasion of Ukraine puts tech giants in an untenable situation. {name}({age:d}), the CEO of high-end electric vehicle manufacturer Tesla , and incidentally the richest man in the world, finds himself in a delicate position. by {name}.".format(name='Yoo',age=35))

'Python' 카테고리의 다른 글

- 문법 - List  (0) 2022.02.28
- 문법  (0) 2022.02.28
#Conditional #if #elif  (0) 2022.02.28
#number #string #Boolean #Expression #ComparisonOperator #MembershipOperator  (0) 2022.02.28
#positional formating #Named placeholder  (0) 2022.02.27