Python string in two lines

4 ways to create Multiline String in Python

You can represent strings in Python by using multiple ways. Python multi-line string is the way of string statements with multiple lines. It is the best way to present multi-line strings in a well-formatted and arranged manner. In this post, we will explore 4 ways to create Multiline String in Python

Important Points to Remember:

  • In Python brackets, backslash, and triple quotes are use to create mutilines.Users will have to manage use of spaces between the strings manually.
  • In the Python string.join() method handles the spaces between the strings implicitly.It is best way to create mutiline string.
  • In Python indentation rules are not applicable to multiline strings.

1. Triple quotes to create Multiline String

Using the Triple quotes We enclosed string in pair of triple-double quotes ” ” ” or single quotes(‘ ‘ ‘) at beginning and end and assign it to a variable. Python treats all strings enclosed between triple quotes as a single string itself.

Читайте также:  Lru cache in java

In Python triple quotes technique , newline(\n) , tab-space(\t) are considered as part of string.

Synatx

sentence = """ multi line strings """

Program Example

Welcome_note = """Welcome to devenum,How are you, good to see you again. Thanks for visiting us again. We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning..""" print(Welcome_note )
Welcome_note = """Welcome to devenum,How are you,\ngood to see you again.\nThanks for visiting us again.\nWe are creating a mutiline string in python.\nPython is vast used programming lanaguage in Machine Learning.""" print(Welcome_note )
Welcome to devenum,How are you, good to see you again. Thanks for visiting us again. We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning.

2: Using backslash

The backslash is also known as an escape sequence(‘\’). It is used to create multiline strings in python. We can pass the string enclosed in double-quotes separated by escape sequence(‘\’)

Syntax

My_Intro = "string1"\"string2"…..\"stringN"

Program Example:

#Program to Create Python Multiline Strings My_Intro = "Welcome to devenum," \ "How are you." \ "good to see you again." \ "Thanks for visiting us again" \ "We are creating a mutiline string in python" print(My_Intro )
Welcome to devenum,How are you.good to see you again.Thanks for visiting us againWe are creating a mutiline string in python

Splice string into mutiple line Using newline charcater

To create a multiline string we can create a newline character(\n).

#Program to Create Python Multiline Strings My_Intro = "Welcome to devenum,\n" \ "How are you.\n" \ "good to see you again.\n" \ "Thanks for visiting us again.\n" \ "We are creating a mutiline string in python \n" print(My_Intro )
Welcome to devenum, How are you. good to see you again. Thanks for visiting us again. We are creating a mutiline string in python

3. string.join() to create a multiline string

Using the string.join() method we can create a multiline string in Python. We can pass multiple strings as arguments to a string.join() method. These strings should be separated by a comma and enclosed inside in double-quotes.

Читайте также:  Index php get header

Syntax

string.join(("string1","string2",…. "stringN"))

Program Example

multiline_note = ' '.join(( "Welcome to devenum,How are you", "good to see you again.", "Thanks for visiting us again.", "We are creating a mutiline string in python.", "Python is vast used programming lanaguage in Machine Learning.")) print(multiline_note)
Welcome to devenum,How are you good to see you again.Thanks for visiting us again. We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning.

Splice string into mutiple line Using newline charcater

If we need to split a string, we can do it by using the special character (\n).

multiline_note = ' '.join(( "Welcome to devenum,How are you", "good to see you again.\n", "Thanks for visiting us again.\n", "We are creating a mutiline string in python.\n", "Python is vast used programming lanaguage in Machine Learning.\n")) print(multiline_note)
Welcome to devenum,How are you good to see you again. Thanks for visiting us again. We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning.

4: Using brackets() to create multiline strings

By using brackets() we can create multiline strings. We can pass the slice of string to create a multiline string.

Syntax

My_Intro = ("string1""string2" ".." "..""stringN")

Program Example:

My_Intro = ("Welcome to devenum,How are you" "good to see you again." "Thanks for visiting us again." "We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning") print((My_Intro )
I am here to teach you the topic about how to write multiple line strings in python language. And i will be teaching you 4 different techniques to create multiline strings using python

Summary

In this post, We have understood 4 techniques to Create Python Multiline Strings in Python. Now using this technique we can easily generate a muti line string in Python.

Источник

4 Techniques to Create Python Multiline Strings

Python Multiline Strings Thumbnail

A Python multiline string is the most efficient way of presenting multiple string statements in a formatted and optimized manner.

During the program, there may be a need to store long strings, which cannot be done in one line, it makes the code look horrible and not preferable, so the best way to handle this problem is multiline strings.

In this tutorial, we will be focusing on the different techniques that can be used to create Python multiline strings.

Triple quotes to create multiline strings in Python

The triple quotes can be used to display multiline strings in Python.

  • If the input contains string statements with too many characters, then triple quotes can serve us with the need to display it in a formatted way.
  • Everything that comes under the triple quotes is considered as the string itself.
inp_str = """You can find the entire set of tutorials for Python and R on JournalDev. Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles.""" print(inp_str)
You can find the entire set of tutorials for Python and R on JournalDev. Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles.

The above output explains the advantage of the triple quotes as it printed the string in the same order as it is written in the code.

Using backslash (\) for multiline string creation

The escape sequence — backslash (‘\’) is used to create multiline strings in Python.

variable = "string1"\"string2"\"stringN"
  • While creating multiline strings using a backslash(\), the user needs to explicitly mention the spaces between the strings.
inp_str = "You can find the entire set of tutorials for Python and R on JournalDev."\ "Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles."\ "Welcome to AskPython!!" print(inp_str)
You can find the entire set of tutorials for Python and R on JournalDev.Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles.Welcome to AskPython!!

In the above output, we can see that it does not handle spaces between strings. The user has to mention it at the time of declaration of multiline strings.

The string.join() method to build a Python multiline string

Python string.join() method has turned out to be an efficient technique for creating Python multiline strings.

The string.join() method handles and manipulates all the spaces between the strings and the user does not need to worry about the same.

string.join(("string1","string2","stringN"))
inp_str =' '.join(("You can find the entire set of tutorials for Python and R on JournalDev.", "Adding to it", "AskPython has got a very detailed version", "of Python understanding through its easy to understand articles.", "Welcome to AskPython!!")) print(inp_str)
You can find the entire set of tutorials for Python and R on JournalDev. Adding to it AskPython has got a very detailed version of Python understanding through its easy to understand articles. Welcome to AskPython!!

Here we can see that the string contains space even though we have not specified it, this is why string.join() method is highly recommended to create Python multiline strings.

Python round brackets () to create multiline strings

Python brackets can be used to create multiline strings and concatenate strings.

The only drawback of this technique is that the user needs to explicitly mention the spaces between the multiline strings.

variable = ("string1""string2""stringN")
inp_str =("You can find the entire set of tutorials for Python and R on JournalDev." "Adding to it " "AskPython has got a very detailed version " "of Python understanding through its easy to understand articles." "Welcome to AskPython!!") print(inp_str)
You can find the entire set of tutorials for Python and R on JournalDev.Adding to it AskPython has got a very detailed version of Python understanding through its easy to understand articles.Welcome to AskPython!!

In the above output, we can see that spaces are not present between each string, which is a drawback of this method, so it is not recommended.

Summary

  • Python multiline strings are strings split into multiple lines to enhance the readability of the code for the users.
  • Python brackets, backslash, and triple quotes can be used to create multiline strings but here, the user needs to mention the use of spaces between the strings.
  • Python string.join() method is considered a very efficient way to create multiline strings and moreover, the spaces between the strings are implicitly handled by the method.
  • Python indentation rules are not applicable to multiline strings.
  • All the escape sequences such as newline(\n), and tab-space(\t) are considered as a part of the string if the multiline string is created using triple quotes.

Источник

Write a long string on multiple lines in Python

In Python, when using PEP8 code checkers like flake8, an E501 line too long error is raised when one line exceeds 80 characters.

This article explains how to split a long string over multiple lines without including a newline character.

See the following article for various operations related to strings with line breaks.

If you want to wrap or truncate long strings, the textwrap module is useful.

If a line becomes too long due to method chaining, you can break up the line similarly.

Line continuation character in Python: backslash ( \ )

In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, the line is considered to continue on the next line.

Furthermore, if multiple string literals are written sequentially, they are concatenated into one string as follows:

s = 'aaa' 'bbb' print(s) # aaabbb 

Therefore, you can break up a long string into multiple lines as follows:

s = 'https://ja.wikipedia.org/wiki/'\ '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'\ '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E' print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

Note that only string literals (strings enclosed by ‘ or » ) are concatenated when written consecutively. Writing variables consecutively without an operator will raise an error.

s_var = 'xxx' # s = 'aaa' s_var 'bbb' # SyntaxError: invalid syntax 

Use the + operator to concatenate variables, or variables and string literals.

s = 'aaa' + s_var + 'bbb' print(s) # aaaxxxbbb 

The + operator is required to concatenate variables, even if they are separated by a backslash ( \ ).

s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\ + s_var\ + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

For more details on string concatenation, see the following article:

Use parentheses for line continuation

In Python, you can freely break lines inside parentheses ( () , <> , [] ). Using this rule, you can split a long string across multiple lines using parentheses instead of backslashes.

Since <> is used for sets and [] is used for lists, use () for this purpose. Note that tuples are created by commas, not () .

s = ('https://ja.wikipedia.org/wiki/' '%E3%83%97%E3%83%AD%E3%82%B0%E3%83' '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E') print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

If variables are included, you need the + operator.

s_var = 'xxx' s = ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + s_var + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb') print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

Источник

Оцените статью