Is this a bug of str.join?

Terry Reedy tjreedy at udel.edu
Wed Feb 16 18:01:44 CET 2011


On 2/16/2011 1:32 AM, fireinice wrote:
> On Feb 16, 1:24 am, fireinice<zhzhqi... at gmail.com>  wrote:
>> Hi, all
>> I'm just working around to generate some fake file for parsing. and I
>> notice some weired thing happen.
>>      time = str(random.randint(1000000000000000, 9999999999999999))
>>      s_id = str(random.randint(1000000000000000, 9999999999999999))
>>      p_id = str(random.randint(1000000000000000, 9999999999999999))
>>      a_id = str(random.randint(1000000000000000, 9999999999999999))
>>      s = "test"
>>      a = [time, s_id, p_id, a_id, s]
>>      print '\t'.join(a)
>>
>> the output is:
>> 3107903582321032        6101282916386924        719897196119318 1780339444980186test
>>
>> you can notice that there is no tab between a_id and s
>> if I switch a_id and p_id, it still happen, but if I delete one of
>> ids, the problem gone.
>> I tried this with python2.6 from debian source and python2.3 which I
>> compiled from source. the result are both the same.
>> What happened to str.join?
>> thanks
>
> I'm sorry, I found it should be the terminal width caused visual
> problem, please kindly ignore this post.

For future reference, the way to get more info about what is really in a 
string is to print its repr(), or even its list() version. Len() is also 
helpful. On Windows, both the Command Prompt window and IDLE refuse to 
expand tabs, so

 >>> s='\t'.join(('a','b','c'))
 >>> str(s)
'a\tb\tc'
 >>> repr(s)
"'a\\tb\\tc'"
 >>> len(s)
5
 >>> list(s)
['a', '\t', 'b', '\t', 'c']

-- 
Terry Jan Reedy




More information about the Python-list mailing list