Python Continue Statement on Next Line of File
How to wrap long lines in Python?
A string is a collection of characters that can represent a single word or a whole sentence. Unlike Java there is no need to declare Python strings explicitly we can directly assign string value to a literal.
A string in Python is represented by a string class which provides several functions and methods using which you can perform various operations on strings.
In this article, we are going to find out how to wrap long lines in Python.
Using parenthesized line breaks
One way to achieve this is by using parenthesized line breaks. Using Python's inferred line continuation within parentheses, brackets, and braces is the recommended method of wrapping lengthy lines.
You can put an extra pair of parentheses around an expression if required. We have to apply these parenthesized line breaks to the long lines that we would want to wrap.
Example 1
In the example given below, we are taking a string as input and we are wrapping them using a list parenthesized line break.
str1 = "Welcome to Tutroialspoint" print ( "The given string is" ) print (str1) print ( "After wrapping the line" ) res = list (str1) print (res)
Output
The output of the above example is,
The given string is Welcome to Tutroialspoint After wrapping the line ['W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'T', 'u', 't', 'r', 'o', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
Example 2
In the example given below, we are going to take the same example as above but we are taking set as a parenthesized line break
str1 = "Welcome to Tutroialspoint" print ( "The given string is" ) print (str1) print ( "After wrapping the line" ) res = set (str1) print (res)
Output
The output of the above example is,
The given string is Welcome to Tutroialspoint After wrapping the line {'i', 'W', 'o', 'T', 'a', 'p', 'm', 'r', 'l', 'c', ' ', 'u', 'n', 'e', 't', 's'}
Using the '\' operator
You can also wrap long lines in Python using the '\' operator. It looks better if you use a backslash. Make sure the continuation line is properly indented. Breaking around a binary operator is best done after it, rather than before it. We have to carefully indent near the backlash, it has to be in between 2 different lines.
Example
In the example given below, we are taking a string as input and we are wrapping the line using the backslash operator.
str1 = """This s a really long line, but we can make it across multiple lines.""" print ( "The given string is " ) print (str1) print ( "Wrapping the given input" ) res = 'This s a really long line,' , \ 'but we can make it across multiple lines.' print (res)
Output
The output of the above example is,
The given string is This s a really long line, but we can make it across multiple lines. Wrapping the given input ('This s a really long line,', 'but we can make it across multiple lines.')
Updated on 26-Oct-2022 07:52:45
- Related Questions & Answers
- How to write long strings in Multi-lines C/C++?
- How to wrap python object in C/C++?
- How to allow long and unbreakable words to be broken and wrap to the next line in JavaScript?
- How long does it take to learn Python?
- How to convert Long array list to long array in Java?
- How to match pattern over multiple lines in Python?
- How to eliminate repeated lines in a python function?
- Program to find how many lines intersect in Python
- How to create a long multi-line string in Python?
- Convert long primitive to Long object in Java
- Lines and Indentation in Python
- How to write multiple lines in text file using Python?
- How to delete lines from a Python tkinter canvas?
- How to word-wrap text in Tkinter Text?
- How do I wrap a string in a file in Python?
Source: https://www.tutorialspoint.com/How-to-wrap-long-lines-in-Python
0 Response to "Python Continue Statement on Next Line of File"
Post a Comment