[Python] Multi-line string assignment

Long character strings may be assigned to variables in SQL operations, etc., but there are mainly the following two methods.

--Parentheses () → Use implicit line continuation --Use of triple quotes ''' and " ""

[Kosei Kitahara's Works: Google Python Style Guide] According to (http://works.surgo.jp/translation/pyguide.html#id73), the former is recommended. The latter is because if you indent as follows, the space will be included in the character string.

<!---

-->

Parentheses () → Use implicit line continuation

text = ('a' #No comma required
        'b')
print(text) # ab

#Same as below
text = 'ab'

#Note 1:If you add a comma, it will be recognized as a tuple of two strings.
text = ( 'a',
         'b')
print(text) # ('a', 'b')

#Note 2:I don't know if it's practical, but it can be put together in one line
text = ('a' 'b')
print(text) # ab

#Note 3:The practicality of this is also unknown, but it is also possible to use line continuation and tuple together.
text = ('a', 'b'
        'c')
print(text) # ('a', 'bc')

\ N is required for line breaks. This method is also effective not only for character string assignment but also for function import. (Do you use it naturally when the number of function arguments increases?).

from some.deep.module.inside.a.module import (
    a_nice_function, another_nice_function, yet_another_nice_function)

[Python Hitchhiking Guide-Code Style] (https://python-guideja.readthedocs.io/ja/latest/writing/style.html#id8)

Combined use with format

When using this together with format, you need to write .format () together at the end:

text = ( 'a = {0}, '
         'b = {1}'.format(1, 2))
print(text) # a = 1, b = 2

Writing .format () on each line will result in SyntaxError:

#The following is a Syntax Error:becomes invalid syntax
text = ( 'a = {}, '.format(1)
         'b = {}'.format(2))

If the contents of .format () become too complicated, is it better to create a list of strings → join withstr.join ()?

<!---

-->

Use of triple quotes ''' and " ""

It is also called "triple quotation mark" or "triple quotation mark". In this case, the line breaks at the line break in the code.

text = '''hello
world'''
print( text )
# hello
# world

#Same as below
text = 'hello\nworld'

#Note 1:If you don't want to break\use
text = '''hello\
world'''
print( text ) # helloworld

#Note 2:If you break the beginning and the end, the line break will be included.
text = '''
hello
world
'''
print( text )
# (new line)
# hello
# world
# (new line)

#Note 3:If you try to match the indentation on the code, the space will be treated as a string.
text = '''hello
          world'''
print( text )
# hello
#           world

Let's programming: multi-line string description using triple quotes note.nkmk.me: String generation in Python (quotes, str constructor)

Recommended Posts

[Python] Multi-line string assignment
Python string
Python string format
python string slice
Python2 string type
Python string format
Python # string type
Python string inversion
Python variable length assignment?
String manipulation in python
Python string manipulation master
[Python2] Date string-> UnixTime-> Date string
Random string generation (Python)
Python3> documentation string / docstring
Python string processing illustration
[python] Convert date to string
Python indentation and string format
String object methods in Python
[Python] Use a string sequence
Various Python built-in string operations
[Python 2/3] Parse the format string
About Python string comparison operators
String date manipulation in Python
Python f character (formatted string)
String format with Python% operator
Python> Read from a multi-line string instead of a file> io.StringIO ()
Python
String replacement with Python regular expression
Multi-line size specification reading with python
6 ways to string objects in Python
python string processing map and lambda
Create a random string in Python
Conversion of string <-> date (date, datetime) in Python
(Java, JavaScript, Python) Comparison of string processing
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Python> link> 2D array initialization and assignment
# 5 [python3] Extract characters from a character string
2015-03-25 python> Multi-line comment> Watch out for indent
[Python] Use string data with scikit-learn SVM
python string comparison / use'list'and'in' instead of'==' and'or'
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
Basic grammar of Python3 system (character string)
[python] EOL while scanning string literal resolution
Generate a class from a string in Python
[Python] Write multi-line plots on Plotly Express
python> link> remove CR, LF>'test string \ n'.rstrip ()
Python basic course (4 numeric type / character string type)
String → Bool value conversion in Python Consideration
A memorandum of python string deletion process