[Python] 01. PRINT

Min's Study Note·2023년 11월 2일

Python

목록 보기
1/21
post-thumbnail

01. PRINT

🔳 특수문자 출력하기

print(!@#$%^&*(\'"<>?:;)

백슬래시가 출력되지 않는다

👉 방법1
문자 앞에 \ (백슬래시)를 붙혀준다

print('!@#$%^&*(\\\'"<>?:;')

👉 방법2
문자열 앞에 r을 붙이면 자동으로 특수문자를 \특수문자 로 치환하여 처리해준다

print(r'!@#$%^&*(\'"<>?:;')

🔳 여러 문자열 출력하기

print("=== 특수문자 ===")
print("\\")   #\
print("\t")   #
print("\'")   #'
print('\"')   #"
print("'Hello'", '"World"')   #'Hello' "World"
print("\"Hello\"", '\'World\'')   #"Hello" 'World'
print("hello \n world")  
#hello
#   world
print("hello \t world")   #hello 	 world

🔳 여러 줄 입력하기

걸음걸음도 살며시 달님이 오시네
""")
#저 산자락에 긴 노을 지면
#걸음걸음도 살며시 달님이 오시네

print(1,2,3,4,5,sep="😊")    #1😊2😊3😊4😊5

🔳 range(시작숫자, 종료숫자, step)

for i in range(10, -1, -1):
  print(i, end=' \n')
  time.sleep(1)
# 10 
# 9 
# 8 
# 7 
# 6 
# 5 
# 4 
# 3 
# 2 
# 1 
# 0 

🔳 printf처럼 사용하기

print("%d %s %3.2f %c" % (100, "LMH", 1234.5678, 48))   
#100 LMH 1234.57 0.

🔳 파일 생성

with open('./beautiful_country.txt', 'w') as f:
  print("""저 산자락에 긴 노을 지면 
  걸음걸음도 살며시 달님이 오시네
  """, file=f)
f = open('beautiful_country.txt', 'r')
##한 줄씩 출력
lines = f.readlines()
for line in lines:
  print(line)

🔳 문자열이 길 때 \를 이용하여 다음줄에 표기

print("첫번째\
두번째\
세번째")
#첫번째두번째세번째

print("""첫번째
두번째
세번째""")
# 첫번째
# 두번째
# 세번째

🔳 .format() 함수

print('{} {} {}'.format(10,20,30)) #중괄호갯수 = 변수갯수  #310 20 30
print("{:+d}".format(52))   #+52
print("{:d}".format(-52))   #-52
print("{:+5d}".format(52)) #부호가 숫자 바로 앞에 위치   #  +52
print("{:5d}".format(-52))    #  -52
print("{:=+5d}".format(52)) #부호가 줄의 앞에 위치   #+  52
print("{:=5d}".format(-52))   #-  52
print("{:+05d}".format(52)) #빈자리를 0으로 채움    #+0052
print("{:#5d}".format(-52)) #빈자리를 #(공백)으로 채움    #  -52
print("{:f}".format(52.273))    #52.273000
print("{:15f}".format(52.273)) #소수포함 15자리   #      52.273000
print("{:+15f}".format(52.273)) #소수포함 15자리    #     +52.273000
print("{:+015f}".format(52.273)) #소수포함 15자리   #+0000052.273000
print(f'{" f ":=^20}')    #======== f =========
print("{0:=^20}".format(' format '))    #====== format ======
print("({0:<10})".format('hi'))   #(hi        )
print("({0:>10})".format('hi'))   #(        hi)
print("({0:^10})".format('hi'))   #(    hi    )
print("({0:0>8})".format('10'))   #00000010)
print("({0:0.4f})".format(3.141592))    #(3.1416)
print("({0:10.4f})".format(3.141592))   #(    3.1416)
print("{{ }}는 클래스이다.".format())   #{ }는 클래스이다.

city = 'Busan'
print(f"나의 살던 고향은 \"{city}\" '부산 ") #Python 3.6부터 가능    #나의 살던 고향은 "Busan" '부산
d = {'city':'부산', 'gu':'부산진구'} #딕셔너리 활용
print(f"내가 일하는 곳 \"{d['city']} {d['gu']}\"") #Python 3.6부터 가능   #내가 일하는 곳 "부산 부산진구"

🔳 기타 문자열 함수

print("hello world".count('l'))   #3
print("hello world".find('l')) #없으면 -1 반환   #2
print("hello world".rfind('l')) #없으면 -1 반환   #9

print("hello world".index('l')) #없으면 error발생    #2
print(",".join('12345'))    #1,2,3,4,5
print('hello'.upper())    #HELLO
print('WORLD'.lower())    #world
print('hello world'.capitalize()) #첫글자만 대문자   #Hello world
l = 'hello world'.split(' ') #split후에는 list자료형이 됨
for i in l:
  print(i.capitalize(), end=" ")   #Hello World
print()
print('   strip word   '.strip())   #strip word
print('   strip word   '.lstrip())    #strip word
print('   strip word   '.rstrip())    #   strip word

(참고) 숫자형
(참고) 문자열 자료형

0개의 댓글