Python 改行: 改行なしで印刷する方法 Python
⚡ スマートサマリー
Python print() はデフォルトで改行を追加しますが、それを回避できます。ここでは、end パラメータ、 Python 2 つのカンマ、sys.stdout.write()、およびループを使用して、出力を 1 行に維持します。

Python print()組み込み関数は、コマンドプロンプト内の指定されたコンテンツを印刷するために使用されます。 Python print の特徴は、最後に改行文字を追加することです。
通常の print() 関数の動作
print() 関数は、コマンド プロンプトまたはコンソールにコンテンツを表示するために使用されます。
これは、 Python 改行機能なしで印刷します。
print("Hello World") print("Welcome to Guru99 Tutorials")
出力:
Hello World Welcome to Guru99 Tutorials
In the given output, you can see that the strings that we have given in print() are displayed on separate lines. The string "Hello World " is printed first, and "Welcome to Guru99 Tutorials" is printed on the next line.
改行なしで印刷する方法 Python?
Python 3+ では、print() に end= という追加パラメータが導入されました。このパラメータは、print() でデフォルトで追加される改行を削除します。
Python 3 改行なしで印刷する以下の例では、文字列を同じ行に印刷します。 Python. To get that working just add end="" inside print() as shown in the example below:
print("Hello World ", end="") print("Welcome to Guru99 Tutorials")
出力:
Hello World Welcome to Guru99 Tutorials
期待通りの出力が得られましたが、文字列間にスペースがありません。文字列 Hello World と Welcome to Guru99個のチュートリアルが、余白なしでまとめて印刷されています。
In order to add space or special character or even string to be printed, the same can be given to the end="" argument, as shown in the example below.
print("Hello World ", end=" ") print("Welcome to Guru99 Tutorials")
So I here we have added one space to the end argument, for example (end=" "). Now, if you see the output, you should see a space between Hello World and Welcome to Guru99チュートリアル。
出力:
Hello World Welcome to Guru99 Tutorials
It's not only space that you can give to the end argument, but you can also specify a string you wish to print between the given strings. So here is an example of it
print("Hello World ", end="It's a nice day! ") print("Welcome to Guru99 Tutorials")
出力:
Hello, World It's a nice day! Welcome to Guru99 Tutorials
改行なしで印刷 Python 2.x
改行なしで文字列を印刷するには、python2.x で、以下に示すように print ステートメントの最後にコンマ (,) を追加する必要があります。
print "Hello World ", print "Welcome to Guru99 Tutorials."
出力:
Hello World Welcome to Guru99 Tutorials
使い方 Python システムモジュール
改行なしで印刷できるもう一つの方法は、 Python 組み込みモジュールは SYS.
これは、を使用する方法を示す実際の例です。 システム 改行なしで印刷するモジュール Python 文字列。
sys モジュールを操作するには、まずモジュールをインポートします。 SYS インポート キーワード。 次に、内部で利用可能な stdout.write() メソッドを利用します。 システム モジュールを使用して文字列を出力します。
import sys sys.stdout.write("Hello World ") sys.stdout.write("Welcome to Guru99 Tutorials")
出力:
Hello World Welcome to Guru99 Tutorials
print() を使用して改行なしでリストを印刷する
Consider a list of items for example: mylist = ["PHP", JAVA", "C++", "C", "PHYTHON"] and you want to print the values inside the list using forループ。 したがって、ここでは、以下の例に示すように、print() を使用してリスト内の値を表示できます。
mylist = ["PHP", "JAVA", "C++", "C", "PHYTHON"] for i in mylist: print(i)
出力:
PHP JAVA C++ C PHYTHON
出力にはリスト項目が次々に新しい行に印刷されます。リストのすべての項目を同じ行に表示したい場合はどうすればよいでしょうか。そのためには、print() 内の end 引数を使用して、新しい行を削除します。 Python リストのすべての項目を同じ行に出力します。
mylist = ["PHP", "JAVA", "C++", "C", "PYTHON"] for i in mylist: print(i, end=" ")
出力:
PHP JAVA C++ C PHYTHON
スター(*)パターンを改行とスペースなしで印刷する
の例 Python 改行なしで印刷すると、 print()関数 forループを使用して同じ行に星(*)を出力します。
for i in range(0, 20): print('*', end="")
出力:
********************
