Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 705477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:03:29+00:00 2026-05-14T04:03:29+00:00

Here is the situation: I’ve got a command I’m running: import subprocess foo =

  • 0

Here is the situation:

I’ve got a command I’m running:

import subprocess
foo = subprocess.Popen('ls /', shell=True, stdout=subprocess.PIPE,\
stderr=subprocess.STDOUT)

Pretty basic, right? And I’ve figured out that I can do stuff with the output with .communicate(), like this:

print foo.communicate()

Which works great and produces the output that the documentation for subprocess.communicate suggests it should, a tuple:

('bin\nboot\ncdrom\n[...stuff redacted for brevity...]tmp\nusr\nvar\nvmlinuz\n', None)

Notice the \n newlines in there. And I’ve discovered that just asking for the first element of the tuple produces output with newlines, like this:

>>> print foo.communicate()[0]
bin
boot
cdrom
[...stuff redacted for brevity...]
tmp
usr
var
vmlinuz

But what I don’t understand is WHY printing with just the first element produces the newlines. Don’t get me wrong, it is great, and I’m glad I can do it without a loop, but I’d like to understand what is going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:03:30+00:00Added an answer on May 14, 2026 at 4:03 am

    In Python when you call something like print obj, you’re actually calling the __str__ on the object and then interpreting the result as a string.

    So for example say you have:

          >>> a = ('foo\nbar', '5')
          >>> print a
          ('foo\nbar', '5')
    

    As you’ve noticed. But if you do something like:

          >>> class mytuple(tuple):
                  def __str__(self):
                      return ''.join(self)
          >>> b = mytuple(a)
          >>> print b
          foo
          bar5
    

    This subclass of the tuple type goes beyond the standard __str__ and builds a string composed out the string representations of its constituent parts.

    The reason this isn’t done in the general case is that you never know what objects could be inserted in a tuple, or list – not all objects have string-able representations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You will have to link the tables together using JOINS… May 14, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer One of my favourite techniques is to put a file… May 14, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer $page_numbers is an integer in both examples. Something else is… May 14, 2026 at 9:02 am

Related Questions

Here is the situation: I have been called upon to work with InstallAnywhere 8,
Here is the situation: I have a table value function with a datetime parameter
Here is the situation: I've got a linux binary that is crashing. No log
Here is the situation: I have a database of 'tickets', and we track changes
Here is the situation. I have 3 tables, one super type, and two sub

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.