1

I am getting the absolute weirdest Python behavior possible and I cannot for the life of me figure it out. With any luck, someone out here has an idea of what's going on.

I am looping the following method:

def request_hiscores_for(name):
  print '+++++ request_hiscores_for() running +++++'+name
  print type(name)
  print '  Requesting hiscores for $name using request_hiscores_for()'.replace('$name',name)
  print '  Requesting hiscores for '+name+' using request_hiscores_for()'
  print '  Requesting hiscores for $name using request_hiscores_for()'.replace('$name','BLA')
  print '----- request_hiscores_for() ending ------\n'

and am getting output along the following lines:

+++++ request_hiscores_for() running +++++10032
<type 'str'>
 using request_hiscores_for()32
 using request_hiscores_for()32
  Requesting hiscores for BLA using request_hiscores_for()
----- request_hiscores_for() ending ------

+++++ request_hiscores_for() running +++++123 Lvs 2 Go
<type 'str'>
 using request_hiscores_for() Lvs 2 Go
 using request_hiscores_for() Lvs 2 Go
  Requesting hiscores for BLA using request_hiscores_for()
----- request_hiscores_for() ending ------

I'd like to point out how the string concatenation is quite literally making parts of the string disappear, and somehow concatenating the arguments in a different order. So... help would be appreciated.

5
  • 2
    use str.format(), it will make your life whole lot eaiser. Commented Sep 1, 2015 at 17:30
  • Where are you getting the name from? Commented Sep 1, 2015 at 17:31
  • the function itself doesn't seem to have any issues. make sure you send in string to it. Commented Sep 1, 2015 at 17:32
  • Use '{}'.format(name) or %s' %(name) Commented Sep 1, 2015 at 17:32
  • When I pass '10032' I do not get any logical error. Unable to reproduce your problem. Commented Sep 1, 2015 at 17:34

1 Answer 1

5

Most probably your name variable's internal representation looks something like -

'10032\r'

Example to show that above representation (especially with the \r at the end , without the \n would cause the issue) -

In [17]: name = '10032\r'

In [19]: print('  Requesting hiscores for $name using request_hiscores_for()'.replace('$name',name))
 using request_hiscores_for()32

In [20]: name = '123 Lvs 2 Go\r'

In [21]: print('  Requesting hiscores for $name using request_hiscores_for()'.replace('$name',name))
 using request_hiscores_for() Lvs 2 Go

\r is carriage return , it brings the cursor back to the start of the line , so any new characters that are print after that starting printing from the start of the line, therefore overwriting whatever was printed previously.

Most probably, wherever you are reading the names from, you are either directly getting the above string, or you are getting something like -

name = '10032\r\n'

But you are either splitting or stripping off \n alone, example -

In [22]: name = '10032\r\n123 Lvs 2 Go\r\n'

In [23]: name.split('\n')
Out[23]: ['10032\r', '123 Lvs 2 Go\r', '']

If you are doing the spliting based on \n , then do not do that, instead use - .splitlines() . Example -

In [24]: name.splitlines()
Out[24]: ['10032', '123 Lvs 2 Go']

If you are doing something like .strip('\n') , then don't do that, use .strip() alone to remove all whitespaces or .strip('\r\n')


Also, it would be better to use string formatting, rather than your replace method. Something like -

print '  Requesting hiscores for {name} using request_hiscores_for()'.format(name=name)
Sign up to request clarification or add additional context in comments.

2 Comments

This was indeed the case - confirmed it when I took a look at the raw string representations of my input! I had a feeling there was some kind of metachar in there, but had absolutely no idea what in the world could have been causing the kind of behavior I was seeing.
seriously now, you saved my live, i was searching the whole internet cause i had exactly the same problem...some variables used to remove everything i had before them and I couldn't find a better solution other than encoding and decoding (common problem when i was concatenating with ciphertexts).. this solved all of my problems..thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.