<p>I have a function where I need to do something to a string.  I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string.  </p>

<p>In C#, I would use an out parameter for the string, but there is no equivalent in Python.  I'm still very new to Python and the only thing I can think of is to return a tuple with the boolean and modified string.</p>

<p>Related question: <a href="https://stackoverflow.com/questions/61605/is-it-pythonic-for-a-function-to-return-multiple-values">Is it pythonic for a function to return multiple values?</a></p>

[{'answer_id': 38513, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 3, 'selected': False, 'text': '<p>Returning a tuple is the usual way to do this in Python.</p>\n'}, {'answer_id': 38514, 'author': 'rmmh', 'author_id': 3694, 'author_profile': 'https://Stackoverflow.com/users/3694', 'pm_score': 4, 'selected': False, 'text': '<p>Return a tuple.</p>\n\n<pre><code>def f(x):\n    # do stuff\n    return (True, modified_string)\n\nsuccess, modified_string = f(something)\n</code></pre>\n'}, {'answer_id': 38516, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 8, 'selected': True, 'text': '<pre><code>def f(in_str):\n    out_str = in_str.upper()\n    return True, out_str # Creates tuple automatically\n\nsucceeded, b = f("a") # Automatic tuple unpacking\n</code></pre>\n'}, {'answer_id': 38524, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 5, 'selected': False, 'text': "<p>Why not throw an exception if the operation wasn't successful?  Personally, I tend to be of the opinion that if you need to return more than one value from a function, you should reconsider if you're doing things the right way or use an object.</p>\n\n<p>But more directly to the point, if you throw an exception, you're forcing them to deal with the problem.  If you try to return a value that indicates failure, it's very well possible somebody could not check the value and end up with some potentially hard to debug errors.</p>\n"}, {'answer_id': 38760, 'author': 'tuxedo', 'author_id': 3286, 'author_profile': 'https://Stackoverflow.com/users/3286', 'pm_score': 2, 'selected': False, 'text': "<p>Throwing an exception for failure is one good way to proceed, and if you're returning a lot of different values, you can return a tuple.  For the specific case you're citing, I often take an intermediate approach: return the modified string on success, and return None on failure.  I'm enough of an unreconstructed C programmer to want to return a NULL pointer to char on failure.</p>\n\n<p>If I were writing a routine to be used as part of a larger library and consumed by other developers, I'd throw an exception on failure.  When I'm eating my own dogfood, I'll probably return different types and test on return.</p>\n"}, {'answer_id': 63481276, 'author': 'Sanmitha Sadhishkumar', 'author_id': 13827419, 'author_profile': 'https://Stackoverflow.com/users/13827419', 'pm_score': 0, 'selected': False, 'text': '<p>You can use return statement with multiple expressions</p>\n'}]
['https://Stackoverflow.com/questions/38508', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3880/']