logo

Python - 여러 줄 문

이 기사에서는 Python 프로그래밍 언어의 Multi-Line 문의 개념을 이해하겠습니다.

Python의 명령문:

Python에서 명령문은 Python 인터프리터가 읽고 실행할 수 있는 논리적 명령입니다. 이는 대입문이거나 Python의 표현식일 수 있습니다.



Python의 여러 줄 문:

Python에서 명령문은 일반적으로 한 줄로 작성되며 이 줄의 마지막 문자는 개행 문자입니다. 명령문을 하나 이상의 행으로 확장하려면 중괄호 {}, 괄호(), 정사각형 [], 세미콜론 ; 및 연속 문자 슬래시 를 사용할 수 있습니다. 코드의 요구 사항에 따라 이들 중 하나를 사용할 수 있습니다. 줄 연속 문자를 사용하면 긴 명령문을 여러 줄()로 명시적으로 나눌 수 있습니다.

암호:

파이썬3








# Initialize the lines using continuation character> g>=> 'geeks> for>> geeks'> print>(g)>

>

>

위 코드에서 연속 문자를 사용하지 않으면 코드는 종료되지 않은 문자열 리터럴 오류를 발생시킵니다.

산출:

geeksforgeeks>

줄 연속은 두 가지 방법으로 나뉩니다.

  • 명시적인 줄 연속
  • 암시적 줄 연속

(명시적 줄 연속) 사용:

이러한 유형의 여러 줄 문에서는 줄 연속 문자()를 사용하여 문을 여러 줄로 분할합니다.

예:

이 예에서는 Python 프로그래밍에서 여러 줄에서 동일한 줄을 계속하기 위해 명시적인 줄 연속인 '' 기호를 사용하여 텍스트와 수학 표현식을 초기화합니다.

파이썬3




# Initializing a text using the> # Explicit multi-line statement.> text>=> 'A Computer Science portal> for> geeks. It contains well written, well> >thought>and> well explained> >computer science>and> programming> >articles'> print>(' Initializing a text using> >the Explicit multi>->line statement', text)> # Initializing a mathematical expression> # using the Explicit multi-line statement.> add>=> 50> +> > >40> -> > >52> print>(' Initializing a mathematical expression> >using the Explicit multi>->line statement', add)>

자바에서 문자를 문자열로 변환
>

>

산출:

Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 38>

괄호 사용(암시적 줄 연속):

이러한 유형의 여러 줄 문에서는 괄호( ), 대괄호 [ ] 및 중괄호 { }를 사용하여 문을 분할할 때 암시적 줄 연속이 사용됩니다.

예:

이 예에서는 Python 프로그래밍에서 여러 줄에서 동일한 줄을 계속하기 위해 암시적 줄 연속인 괄호( ), 대괄호 [ ] 및 중괄호 { } 기호를 사용하여 목록과 수학 표현식을 초기화합니다.

스크립트 쉘 실행

파이썬3




# Initializing a string> # using parentheis '()'.> g>=> (f>'geeks'> >f>'for'> >f>'geeks'>)> print>(g)> # Initializing a list using the> # Implicit multi-line statement.> list> => [>5>,> >4>,>3>,>2>,>1> >]> print>()> print>('Initializing a>list> using the> >Implicit multi>->line statement',>list>)> # Initializing a mathematical expression> # using the Implicit multi-line statement.> add>=> (>50> +> >40> -> >52>)> print>()> print>('Initializing a mathematical expression> >using the Explicit multi>->line statement', add)>

>

>

산출:

geeksforgeeks Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 38>

삼중따옴표(줄 바꿈) 사용

예:

파이썬3




# Initializing a string> # using triple qoute.> g>=> '''geeks> for> geeks'''> print>(g)> print>()> print>(f>'escape charactor: {g!r}'>)>

>

>

산출

geeks for geeks escape charactor: 'geeks
for
geeks'>