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

  • SEARCH
  • Home
  • 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 9226109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:45:01+00:00 2026-06-18T04:45:01+00:00

I observed a strange behaviour of the __future__ module’s print_function in Python 3.2. Take,

  • 0

I observed a strange behaviour of the __future__ module’s print_function in Python 3.2.

Take, for example this code:

from __future__ import print_function
import sys

print('Enter the base path of the images: ', end='')
path = sys.stdin.readline().strip().strip('"')
if len(path) == 0:
    print("No path entered")
else:
    print(root)
print("\n\nPress ENTER to exit")
exit = sys.stdin.readline()

When the script is run, it appears the console waits for the user to press ENTER before displaying the first print statement.
The output then looks like this:


Enter the base path of the images: No path entered


Press ENTER to exit

Needless to day, displaying an empty prompt to the user leads to a lot of confusion especially since a lot of people are afraid of the black window with white text (Command Prompt).

When the code is changed to this

from __future__ import print_function
import sys

print('\nEnter the base path of the images: ', end='') #line now starts with \n
path = sys.stdin.readline().strip().strip('"')
if len(path) == 0:
    print("No path entered")
else:
    print(path)
print("\n\nPress ENTER to exit")
exit = sys.stdin.readline()

Then the output is as expected (assuming we ignore the preceding empty line):


Enter the base path of the images: c:\
c:\


Press ENTER to exit

When the code is run in python 2.6 however, the first one works as expected (i.e. it displays Enter the base path of the images: before waiting to receive input).

This leads me to ask:
Why do I need to precede the print function with a \n in order to get an output displayed in Python 3.2 while I don’t need the \n when running in Python 2.6?
Could it be that the print_function is implemented differently in the two versions?

  • 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-06-18T04:45:02+00:00Added an answer on June 18, 2026 at 4:45 am

    You are seeing the effects of line buffering. Flush stdout first (using sys.stdout.flush() for backwards compatibility with Python 2):

    print('Enter the base path of the images: ', end='')
    sys.stdout.flush()
    

    The print() function in Python 2 is certainly different from the one in Python 3 (where the from __future__ import print_function line is effectively meaningless). In Python 3, I/O has been overhauled and stdout buffering semantics have changed subtly. In Python 2, the sys.stdin.readline() call flushes stdout automatically, in Python 3 this is no longer the case.

    If you use the input() function instead of reading from stdin directly, you do not need to flush at all:

    msg = 'Enter the base path of the images: '
    try:
        # python 2
        path = raw_input(msg)
    except NameError:
        # python 3
        path = input(msg)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I observed this strange behaviour of center method of str objects in python 2.7
I've first observed this issue in a production code, then made a prototype: import
I have observed a strange behavior in running the same code in a Matlab
I am observing a strange behaviour. The following code that is related to conversion
I'm working on a WebCenter Spaces application and have observed some strange behaviour in
I observed a strange problem in a piece of code where an adhoc SQL
I just observed a strange behaviour (of course Oracle is probably supposed to behave
I observed this strange problem. Inserting with now() is fine, but when adding a
Trying to understand upcasting in Java. Recently observed strange behavior. Example: public class A
I observe the following strange behaviour in Vim 7.3: When I'm below a fold,

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.