[Python] 파이썬 입력과 출력 (print)

AhnHz·2023년 11월 25일
0

Python 기초

목록 보기
4/8
post-thumbnail

1. 화면 출력

기본 출력

print('Hello Word')

>> Hello World
print('I', 'love', 'Python', sep = '-')  # - 로 문자열 구분

>> I-love-Python

print("James is my friend.\nHe is Korean.")  # 개행문자 \n

>>	James is a my friend.
	He is Korean.

형식 지정 출력

  • % 를 이용한 형식 및 위치 지정
oper = '반지름'  # 문자 데이터 할당
r = 3  # 정수 데이터 할당
PI = 3.14159265358979 # 실수 데이터 할당

print("%s: %d, 원주율: %f" % (oper, r, PI))

>> 반지름: 3, 원주율: 3.141593

  • 출력 위치 지정
animal_0 = "cat"
animal_1 = "dog"
animal_2 = "fox"

print("Animal: {}, {}, {}".format(animal_0, animal_1, animal_2))
print("Animal: {1},{2},{0}".format(animal_0, animal_1, animal_2))  # 위치 지정

>>	Animal: cat, dog, fox

	Animal: dog, fox, cat

  • 숫자 형식 지정
a = 0.1234567890123456789
print("{0:.2f}, {0:.5f}".format(a))

>> 0.12, 0.123456



2. 키보드 입력

엔터로 입력할 수 있다.

a = input("정사각형 한 변의 길이는?: ")
area = int(a) ** 2

print("정사각형의 넓이: {}".format(area))

>>	정사각형 한 변의 길이는?: 4

	정사각형의 넓이: 16
profile
데이터 분석가 연습생입니다

0개의 댓글