How to write string concatenation in multiple lines in Python

When I checked how to describe the concatenation of strings in multiple lines in Python, there were several patterns, so I tried to sort out the "good patterns" that can be used and the "bad patterns" that cause execution errors.

Good pattern

--String after \ [newline]

val = 'abcde' \
'12345678'

--String after + \ [newline]


val = 'abcde' + \
'12345678'

--Function after + \ [newline]

val = 'abcde' + \
str(12345678)

--The string + function after + \ [newline]

val = 'abcde' + \
'1234' + str(5678)

--Separate with + [line feed] and enclose in parentheses ().

val = ('abcde' +
'12345678')

#Indentation will also be available
val = (
  'abcde' +
  '12345678'
)

Bad pattern

--Function after \ [newline]

val = 'abcde' \
str(12345678)

Execution results in 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"
  ]
}

that's all

Recommended Posts

How to write string concatenation in multiple lines in Python
How to write a string when there are multiple lines in python
How to write Ruby to_s in Python
How to embed a variable in a python string
python> How to write multiple lines> How to write in mnist_with_summaries.py> It seems that there is no need for line concatenation symbols
How to develop in Python
20th Offline Real-time How to Write Problems in Python
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
How to retrieve multiple arrays using slice in python.
How to write a Python class
[Python] How to do PCA in Python
How to write soberly in pandas
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
[Python] How to write an if statement in one sentence.
How to define multiple variables in a python for statement
Python: String concatenation
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to make a string into an array or an array into a string in Python
How to write Python document comments (Docstrings)
[Introduction to Python] How to output a character string in a Print statement
[Itertools.permutations] How to put permutations in Python
The 15th offline real-time how to write reference problem in Python
Send email to multiple recipients in Python (Python 3)
How to get a string from a command line argument in python
How to work with BigQuery in Python
[Python] How to invert a character string
How to write offline real time Solve E04 problems in Python
How to get a stacktrace in python
Difference in how to write if statement between ruby ​​and python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
The 14th offline real-time how to write reference problem in python
How to use regular expressions in Python
How to delete multiple specified positions (indexes) in a Python list
How to display Hello world in python
How to write this process in Perl?
How to use is and == in Python
The 18th offline real-time how to write reference problem in Python
The 17th offline real-time how to write reference problem implemented in Python
How to write the correct shebang in Perl, Python and Ruby scripts
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
[Introduction to Python] How to write a character string with the format function
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python