python에서 print를 이용해 출력하는 방법을 학습했다.
py = '파이썬'
print(1)
print(py)
print(py, 1)
print(py, 1, '점심시간')
결과
> 1
> 파이썬
> 파이썬 1
> 파이썬 1 점심시간
변수를 선언해서 출력할 수도 있고, 변수와 숫자, 문자를 섞어서 출력하는것도 가능하다. 이 때 쉼표(,)로 값을 연결할 수도 있지만 플러스(+) 기호로 연결하는것도 가능한데 주의할 점이 있다.
print(py + 1 + '점심시간')
print(py + 1)
print(py + '점심시간')
print(1 + 2)
print('1' + '2')
num = 2
print(num + 1)
결과
> TypeError: can only concatenate str (not "int") to str
> TypeError: can only concatenate str (not "int") to str
> 파이썬점심시간
> 3
> 12
> 3
데이터 타입이 다른 것들을 +로 연결했을 경우 typeerror가 발생하는 것을 알 수 있다. 이는 변수에도 적용되는데, 세번째 코드를 보면 string이 담긴 변수와 '점심시간'이라는 문자를 연결하면 오류가 나지 않는다. 변수에 숫자 타입이면 숫자를 넣었을 경우 +를 연산자로 인식해 값을 프린트 한다. 단순히 숫자를 연결해서 출력하고 싶다면, 숫자 양 옆에 작은 따옴표(')를 붙여 문자열로 변환하면 된다.
print("What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
> SyntaxError: EOL while scanning string literal
여러줄을 출력하면 오류가 난다.
print('''What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.''')
> What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
작은따옴표나 큰 따옴표를 텍스트 양 옆에 3개 붙여주면 띄어쓰기까지 출력이 된다.
만약 이 때 문장 안에 변수가 있다면 시작하는 작은 따옴표 3개 앞에 f를 붙이고 변수의 양 옆에 {}를 붙여주면 된다.
Lorem_Ipsum = '로렌입숨'
print(f'''{Lorem_Ipsum} is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of {Lorem_Ipsum}.''')
로렌입숨 is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of 로렌입숨.
문장 안에 변수가 여러개 들어간다고 해서 +나 , 를 이용하는 것 보다 훨씬 간단하다.
print('1\n2\n3')
-
print(1\n2\n3)
1
2
3
-
SyntaxError: unexpected character after line continuation character
첫번째의 경우 의도한대로 출력됐지만 두 번째는 문법오류가 발생했다.
\n이 string이기 때문에 따옴표 안에 들어가야 하기 때문이다.
print('it's rainy')
print("it's rainy")
> SyntaxError: invalid syntax
> it's rainy
문장 안에 작은따옴표가 있을 때 문장을 묶는 기호로 작은 따옴표를 쓴다면 문법 오류가 발생한다. 이 때는 큰따옴표를 쓰는것도 방법이겠지만, 다른 방법도 있다.
print('it\'s rainy')
it's rainy
작은 따옴표 앞에 (역슬래시)를 붙이면 모든 문장이 깔끔하게 출력된다.
print(1, 2, 3)
print(1, 2, 3, sep=' ')
print(1, 2, 3, sep='')
print(1, 2, 3, sep='x')
print(1, 2, 3, sep=' x ')
>>> 1 2 3
>>> 1 2 3
>>> 123
>>> 1 x 2 x 3
맨 마지막에 sep을 넣어주면 된다. sep은 seperator의 약자로 구분자 라는 뜻이다. sep 안에 \n을 넣으면 줄바꿈도 가능하다.