Python 改行: 改行なしで印刷する方法 Python

⚡ スマートサマリー

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

  • 🚫 改行なし: 出力が1行に収まるように、endを空文字列に設定します。
  • ➕ カスタムエンド: endには、項目間にスペース、文字列、または記号を挿入できます。
  • 🐍 Python 2: In Python 2.x では、改行をスキップするために末尾にカンマを追加します。
  • 甘いもの<XNUMXxXNUMX><XNUMXxXNUMX><XNUMXxAXNUMX><XNUMXxAXNUMX>️️ システムモジュール: sys.stdout.write() は、改行を追加せずにテキストを出力します。
  • ???? リストを印刷する: リストの末尾をスペースに設定してループ処理を行い、項目を結合します。
  • 🤖 AIによる支援: CopilotのようなAIツールは、終了パターンと印刷パターンを瞬時に提案します。

Python 改行なしの印刷

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="")

出力:

********************

よくあるご質問

In Python 3. print() 関数に end='' を渡します。例えば、print(text, end='') のように指定します。これにより、デフォルトの改行が停止します。

endにスペースを設定するには、print(text, end=' ')を使用します。endに指定した文字列は、改行ではなく項目を区切ります。

In Python 2.x では、print の後にカンマを追加します。例: print 'text',。カンマによって改行が抑制されます。

はい。sys.stdout.write(text) は改行なしで出力します。まず sys をインポートしてください。print() とは異なり、改行は追加されません。

リストをループして、各項目をendをスペースに設定して出力します。 for i in mylist: print(i, end=' ').

GitHub CopilotのようなAIアシスタントは、print()を適切なendパラメータで自動補完し、入力中にsys.stdout.write()を提案します。

はい。AI搭載ツールが末尾パラメータを追加し、変換します。 Python 2枚のプリント Python 3. 出力ループを自動的にリファクタリングします。

print() 関数は、呼び出しごとに改行を追加して、毎回新しい行から開始します。end パラメータはこのデフォルト設定を上書きします。