0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで文字列の連結を複数行で記述する方法

Last updated at Posted at 2020-03-08

Pythonで文字列の連結を複数行で記述する方法を確認したところ何パターンかあったので、利用可能な「良いパターン」と実行エラーとなる「駄目なパターン」を整理してみました。

良いパターン

  • \[改行]の後に文字列
val = 'abcde' \
'12345678'
  • + \[改行]の後に文字列

val = 'abcde' + \
'12345678'
  • + \[改行]の後に関数
val = 'abcde' + \
str(12345678)
  • + \[改行]の後に文字列 + 関数
val = 'abcde' + \
'1234' + str(5678)
  • +[改行]で区切って括弧()で囲む。
val = ('abcde' +
'12345678')

# インデントも使えるようになる
val = (
  'abcde' +
  '12345678'
)

駄目なパターン

  • \[改行]の後に関数
val = 'abcde' \
str(12345678)

実行がSyntaxErrorとなる。

{
  "errorMessage": "Syntax error in module 'hoge': invalid syntax (hoge.py, line 2)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"hoge.py\" Line 2\n        str(12345678)\n"
  ]
}

以上

0
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?