デフォルトでは、Python の print() 関数は改行で終わります。 C/C++ のバックグラウンドを持つプログラマは、改行なしで印刷する方法を疑問に思うかもしれません。 Pythonのprint() 関数には、というパラメータが付いています。 '終わり 「。」デフォルトでは、このパラメータの値は「 」、つまり改行文字です。
例 1:
ここでは、このパラメータを使用して任意の文字/文字列で print ステートメントを終了できます。
Python3
# ends the output with a space> print>(>'Welcome to'>, end>=> ' '>)> print>(>'techcodeview.com'>, end>=> ' '>)> |
>
>
出力:
Welcome to techcodeview.com>
例 2:
の動作をデモするもう 1 つのプログラム 終了パラメータ 。
Python3
knn アルゴリズム
# ends the output with '@'> print>(>'Python'>, end>=>'@'>)> print>(>'techcodeview.com'>)> |
>
>
出力:
[email protected]>
例 3:
print() 関数は sep パラメータを使用して引数を区切り、最後の引数の後で終了します。
Python3
print>(>'G'>,>'F'>, sep>=>'>', end='>')> print>(>'G'>)> #
provides new line after printing the year> print>(>'09'>,>'12'>,>'2016'>, sep>=>'-'>, end>=>'
'>)> > print>(>'Red'>,>'Green'>,>'Blue'>, sep>=>','>, end>=>'@'>)> print>(>'geeksforgeeks'>)> |
>
>
出力
GFG 09-12-2016 Red,Green,Blue@geeksforgeeks>
end を使用して文字列を連結します。
この例では、end パラメータを使用して 2 つの print() ステートメントを 1 行の出力に連結します。最初の print() ステートメントの end パラメーターはスペース文字に設定されているため、2 番目の print() ステートメントはスペース文字で区切られて同じ行で始まります。
end パラメーターは、Python の print() 関数の便利な機能で、さまざまな方法で出力の書式設定を制御するために使用できます。
Python3
name>=> 'Alice'> age>=> 30> print>(>'My name is'>, name,>'and I am'>, age,>'years old.'>, end>=>' '>)> print>(>'Nice to meet you!'>)> |
>
>出力
My name is Alice and I am 30 years old. Nice to meet you!>