Hello, is this a bug? I get different results when I run this from command prompt and IDLE.

name = input('Hello what is your name: ')
print('Hello ' + name + '.')

From IDLE, it works correctly. The result shows as

Hello Bob.

If I run the program from a command prompt, (ie. c:\python32\python.exe name.py) the '.' is going to the front of returning print() statment, it looks like this

.ello Bob


Is this a bug?

vegaseat commented: interesting +13

Recommended Answers

All 7 Replies

Strange!
Works fine with Python31, but not with Python32. You could be on to something!

The egg-heads updated the str() function in Python32 to be more in line with the repr() function.

Strange, working from the python command line, if i declare

name = 'Bob'

then print('Hello ' + name + '.') works correctly showing Hello Bob.

But getting the name from the input() function, its like it takes the 'enter-key' as part of the string.

This is on Windows 7.. hmm

Even stranger ...

name = input('Hello what is your name: ')
s ='Hello ' + name + '.'
print(s[0], ord(s[0]))  # H 72
print(s)                # .ello Bob

It knows that s[0] is 'H', but it still prints '.' in commandline

I have seen someone else in a other forum had the same problem with python 3.2
Is correct when run from IDLE,and the problem is when run from command line.
This is python bug tracker,if you want to look for or report a bug.
http://bugs.python.org/

It seems to have return character from input in:

C:\Python32>python
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input('Hello what is your name: ')
Hello what is your name: Bob
>>> print('Hello ' + name + '.')
.ello Bob
>>> name
'Bob\r'
>>>

The '.' is last character, but is printed as first character in line as it is after the return.

Cool, that makes sense, that was my first hunch, but being new to python/programming I wasnt sure how to check it. Just look at 'name' .. duh :)

Also, I believe I found the bug report for this.

http://bugs.python.org/issue11272

name.py is the following code ...

name = input('Hello what is your name: ')
s ='Hello ' + name + '.'
print("s[0] = %s" % s[0])
print("len(name) = %d" % len(name))
print(s)

''' result using batch file c:\python32\python.exe -u name.py ...
s[0] = H
len(name) = 4
.ello Bob
'''

It knows the first character in string s is 'H' but still prints a '.'
The length of 'Bob' is 4 which implies a return character has been attached.
To make things even goofier it gives the same result with Python31 and Python32
This only happens if you run the code from a batch file.

My advice, don't run Python3 code containing input() from a Windows batch file! Seems to be fine if run from an IDE.

Here is another look at this showing clearly that the trailing '\r' (ASCII 13) character associated with name input is the culprit. For Windows '\r' is just a carriage return not a new line ...

name = input('Hello what is your name: ')
s ='Hello ' + name + '!!'
print("s[0] = %s" % s[0])
print("ord(name[-1]) = %s" % ord(name[-1]))
print("len(name) = %d" % len(name))
print(s)

''' result using batch file c:\python32\python.exe -u name.py ...
s[0] = H
ord(name[-1]) = 13
len(name) = 4
!!llo Bob
'''

And a solution to get rid of that trailing '\r' character ...

name = input('Hello what is your name: ').strip()
s ='Hello ' + name + '!'
print("s[0] = %s" % s[0])
print("len(name) = %d" % len(name))
print(s)

''' result using batch file c:\python32\python.exe -u name.py ...
s[0] = H
len(name) = 3
Hello Bob
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.