3

I'm wondering if there is a function in the standard library that takes whatever the print function would print and returns it as a string. It seems like there ought to be but if it's there I'm not looking in the right spot.

My situation is I've done a programming exercise that creates (after some calculation) a string of digits stored in a list, e,g,

digits=[1, 0, 0, 1, 0, 1, 0, 0, 0, 1]

and the position of a decimal point dp=4

and I want to roll this into something that looks like a decimal. The print function works nicely to display what I want:

print(*(digits[:dp + 1] + ['.'] + digits[dp + 1:]), sep='')

produces 10010.10001

but if I want to return this result as a string is seems I have to write additional code. Not that it would be difficult but it's unnecessary if there's something already built-in. None of print, format or the % operator seem to do the trick. The closest thing I could find was the StringIO class but I was hoping for something a bit simpler and less arcane.

3
  • @BartoszKP: Nah, this is the right question for the wrong answers to that question. Commented Oct 8, 2013 at 22:15
  • but printing to a StringIO buffer is simple and non-arcane. Commented Oct 8, 2013 at 22:26
  • Just an aside, it would be neater to use [(x) for x in digits].insert(dp+1, '.') to add the decimal point Commented Oct 8, 2013 at 22:56

4 Answers 4

5

You can give an alternate buffer to the print statement:

>>> import io
>>> buf=io.StringIO()
>>> print('hello',file=buf)
>>> buf.getvalue()
'hello\n'
>>>
Sign up to request clarification or add additional context in comments.

3 Comments

or print >>buf, "hello"
also, cStringIO is supposedly faster ?
@ychaouche - This is a python3 example where cStringIO and the '>>' print thing have gone away. Judging from the way the poster wrote his print statement, I think he's using python3.
2

You can define your own function which behaves pretty similar to print like this (untested, have no python on tablet):

def print_to_string(*objects, sep=' ', end='\n'):
     return sep.join(str(obj) for obj in objects) + end

The file argument in print does not make a lot of sense for strings.

1 Comment

So I take it that the answer is there is nothing built it, but I could put this in MyLib.py and get essentially the same effect. Thanks.
1

Python3's print function will call write method of the file argument

If you don't like using StringIO, you can write your own:

class StringWrapper:
  def __init__(self): 
    self.string = "";
  def write(self, string):
    self.string += string

This is used with the file argument:

s = StringWrapper()
print("foo", "bar", file=s)
# s.string will be "foo bar\n"

Comments

-1

Check out the join command on strings - first you must convert your digits to strings:

sdigits = [str(i) for i in digits]
''.join(sdigits[:dp+1] + ['.'] + sdigits[dp+1:])
>>> '10010.10001

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.