Skip to content
Fix Code Error

How can I do a line break (line continuation) in Python?

March 13, 2021 by Code Error
Posted By: Ray Vega

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?

For example, adding a bunch of strings,

e = 'a' + 'b' + 'c' + 'd'

and have it in two lines like this:

e = 'a' + 'b' +
    'c' + 'd'

Solution

What is the line? You can just have arguments on the next line without any problems:

a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, 
            blahblah6, blahblah7)

Otherwise you can do something like this:

if (a == True and
    b == False):

or with explicit line break:

if a == True and 
   b == False:

Check the style guide for more information.

Using parentheses, your example can be written over multiple lines:

a = ('1' + '2' + '3' +
    '4' + '5')

The same effect can be obtained using explicit line break:

a = '1' + '2' + '3' + 
    '4' + '5'

Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.

Answered By: Harley

Related Articles

  • Can't install via pip because of egg_info error
  • How can I determine whether a 2D Point is within a Polygon?
  • What's the difference between eval, exec, and compile?
  • useEffect Error: Minified React error #321 (GTM…
  • How to prevent scrolling the whole page?
  • What are the undocumented features and limitations…
  • FPS Projectile firing from the wrong place
  • PHP parse/syntax errors; and how to solve them
  • long long int vs. long int vs. int64_t in C++
  • data.table vs dplyr: can one do something well the…
  • Getting the closest string match
  • How do I merge two dictionaries in a single…
  • AngularJS : Initialize service with asynchronous data
  • Usage of __slots__?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to update Python?
  • Trying to integrate Ant Design using Sass and…
  • What is your most productive shortcut with Vim?
  • What is the difference between "long", "long long",…
  • OpenCL - Approximation of Pi via Monte Carlo…
  • TypeScript metadata reflection references other…
  • Reference - What does this regex mean?
  • return two dimensional array from function with…
  • Smart way to truncate long strings
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Adding numbers and receiving a NaN
  • Access Nuxt plugins in .js files
  • Install pip in docker
  • How to solve Internal Server Error in Next.Js?
  • What are type hints in Python 3.5?
  • What does Ruby have that Python doesn't, and vice versa?
  • How do I wait until Task is finished in C#?
  • Why does C++ code for testing the Collatz conjecture…
  • Problems Installing CRA & NextJS from NPM…
  • How do servlets work? Instantiation, sessions,…
  • Getting started with Haskell
  • Design DFA accepting binary strings divisible by a…
  • Create multiple threads and wait all of them to complete
  • Fastest way to iterate over all the chars in a String
  • How can I make an svg scale with its parent container?
  • Circle line-segment collision detection algorithm?
  • sys.argv[1] meaning in script
  • Start redis-server with config file
  • How to generate a random string of a fixed length in Go?
  • Logging best practices
  • How to filter a RecyclerView with a SearchView
  • Is it possible to send a variable number of…
  • Using Mockito's generic "any()" method
  • Tailswind css - "list-disc" is not styling bullets…
  • How to specify 64 bit integers in c
  • Ukkonen's suffix tree algorithm in plain English
  • The definitive guide to form-based website authentication
  • Call .focus() on a sibling VueJS Component
  • Relative imports for the billionth time
  • How to convert an XML file to nice pandas dataframe?
  • Is it possible to break a long line to multiple…
  • RegEx match open tags except XHTML self-contained tags
  • How does PHP 'foreach' actually work?
  • Live chat scraping (Youtube) with casper. Issue with…
  • Trouble with Next js + Express deployment using Zeit Now
  • Running shell command and capturing the output
  • For-each over an array in JavaScript
  • How can I fix MySQL error #1064?
  • How can I pass a list as a command-line argument…
  • Memcached vs. Redis?
  • Copy a VkImage after TraceRaysKHR to CPU
  • How to pass all arguments passed to my bash script…
  • Are dictionaries ordered in Python 3.6+?
  • What does "Could not find or load main class" mean?
  • Manually raising (throwing) an exception in Python
  • How can I manually compile a svelte component down…
  • Install opencv for Python 3.3
  • how to transform a JSON coming from an API into…
  • How can I find the product GUID of an installed MSI setup?
  • What is the difference between venv, pyvenv, pyenv,…
  • SQL query return data from multiple tables
  • What's the best way of scraping data from a website?
  • multiple matching routes
  • EmberJS Action target an object within an array
  • Switch statement for greater-than/less-than
  • What is the difference between bottom-up and top-down?
  • How to build next.js production?
  • How can I install a previous version of Python 3 in…
  • Using two values for one switch case statement
  • How to deploy next.js on AWS Elastic Beanstalk?
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • Elegant ways to support equivalence ("equality") in…
  • What version of Python is on my Mac?
  • Keras Sequential API is replacing every layer with…
  • "Thinking in AngularJS" if I have a jQuery background?
  • What does "Fatal error: Unexpectedly found nil while…
  • Can i add conditional events on svelte components?
  • Error: Jump to case label
  • Comparing boxed Long values 127 and 128
  • How can I install pip on Windows?
  • How to "properly" create a custom object in JavaScript?
  • Equals(=) vs. LIKE
  • How do Mockito matchers work?
  • Named regular expression group "(?Pregexp)": what…
  • Are 'Arrow Functions' and 'Functions' equivalent /…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Sort array of objects by string property value

Next Post:

How can I replace a newline (n) using sed?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error