Python入門 複数行の代入はできますか?

複数行の代入はできますか?

方法1.三重引用符'''で囲む。

def Python_Multiline_String():
  multiline_string = '''Hello and
  Welcome to
  Python Guide
  '''
  print(multiline_string)
  """ output
  Hello and
  Welcome to
  Python Guide
  """
Python_Multiline_String()

方法2.エスケープ\を使う

#!/usr/local/env python3

def Python_Multiline_String():
  multiline_string = '''Hello and
  Welcome to
  Python Guide
  '''
  print(multiline_string)
  """ output
  Hello and
  Welcome to
  Python Guide
  """ 
  multiline_string = 'Hello and\nWelcome to \nPython\nGuide'
  print(multiline_string)
  """ output
  Hello and
  Welcome to
  Python Guide
  """ 
Python_Multiline_String()

方法3.リストにしてjoin()でつなぐ

#!/usr/local/env python3

def Python_Multiline_String():
  # 方法1
  multiline_string = '''Hello and
  Welcome to
  Python Guide
  '''
  print(multiline_string)
  """ output
  Hello and
  Welcome to
  Python Guide
  """ 
  # 方法2
  multiline_string = 'Hello and\nWelcome to \nPython\nGuide'
  print(multiline_string)

  # 方法3
  lines = ['Hello and', 'Welcome to', 'Python Guide']
  multiline_string = '\n'.join(lines)
  print(multiline_string)
  """ output
  Hello and
  Welcome to
  Python Guide
  """ 

Python_Multiline_String()

Python で複数行の文字列を作成するには、「三重引用符'''」、「エスケープ文字`'」、「join()」メソッドなどのさまざまな方法を使用できます。

書籍の紹介

Python入門 文字列を置換したいのですが?

Python入門 文字列を置換したいのですが?

Python入門 辞書内包表記ってなんですか?

Python入門 辞書内包表記ってなんですか?