
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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) #Using parenthesized line break (list) 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) #Using parenthesized line break (set) 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.' #Wrapping the line using backslash 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.')