2016-12-06 6 views
0

Pythonで1行のコードに複数の関数を出力したいとします。どのように私は\n()、および'を取り除くのですか?私は現在、あなたがprint文の後にコンマを入れてする方法不要な文字を使わずに1行のコードに複数の関数を表示する方法

print("Hello, my name is " + introName + " and I have a " +outroName+.",) 

を使用していますが、それは

このような
('Hello, my name is Arthur\n and I have a secret.',) 

を印刷しますか

+0

あなたは文字列を 'item1、'のように定義できる単一の項目を持つタプルに入れました。 'print("こんにちは、私の名前は "+ introName +"で、 "+ outroName +。") 'のように見えます。 –

+3

答えはどのバージョンのPython2またはPython3を使用していますか?あなたの説明から、あなたはPython2を使用しているようです。 –

答えて

2

introNameには最後に\nが含まれているようですが、strip()を使用して削除できます。

introName.strip('\n') 

あなたの行は次のようになります。

print("Hello, my name is " + introName.strip('\n') + " and I have a " + outroName.strip('\n')) 

より良いが、このようなformat()を使用している:

s = "Hello, my name is {} and I have a {}".format(introName.strip('\n'), outroName.strip('\n')) 
print(s) 

出力:それはそのBのように印刷しています

>>> introName = 'Arthur\n' 
>>> outroName = 'secret' 
>>> 
>>> s = "Hello, my name is {} and I have a {}".format(introName.strip('\n'), outroName.strip('\n')) 
>>> s 
'Hello, my name is Arthur and I have a secret' 
+0

ありがとう、この解決策が働いた –

+0

@Morrisあなたはそれを受け入れることができます:) – ettanany

関連する問題