use str

try:
    some_method()
except Exception as e:
    s = str(e)

Also, most exception classes will have an args attribute. Often, args[0] will be an error message.

It should be noted that just using str will return an empty string if there's no error message whereas using repr as pyfunc recommends will at least display the class of the exception. My take is that if you're printing it out, it's for an end user that doesn't care what the class is and just wants an error message.

It really depends on the class of exception that you are dealing with and how it is instantiated. Did you have something in particular in mind?

Answer from aaronasterling on Stack Overflow
🌐
Linux Hint
linuxhint.com › convert-an-exception-to-a-string-in-python
How Do I Convert an Exception to a String in Python – Linux Hint
To convert an exception to a string in Python, apply the “str()” function, “traceback.format_exc()” function, or the “repr()” function. All of these functions are used combined with the “try-except” blocks to catch and convert the faced exception into a string. This guide presented various methods to convert an exception to a string in Python using numerous examples.
🌐
Embedded Inventor
embeddedinventor.com › home › python exception to string
Python Exception to string
September 27, 2023 - You can do so by passing in the string to be printed along with the constructor as follows. try: raise IndexError('Custom message about IndexError') except Exception as e: print(e) ... To understand how the built-in function print() does this magic, and see some more examples of manipulating these error messages, I recommend reading my other article in the link below. Python ...
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
>>> 10 * (1/0) Traceback (most ... "int") to str · The last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example are ZeroDivisionError, NameError and TypeError. The string printed as ...
🌐
Iditect
iditect.com › faq › python › converting-exception-to-a-string-in-python.html
Converting Exception to a string in Python
Here's how you can do it: ... try: ... the exception to a string exception_string = str(e) print("Exception as a string:", exception_string) In this example, we catch an exception and then use str(e) to convert the exception to a string....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exception-handling
Python Exception Handling - GeeksforGeeks
Catch-all handler is used to call to catch any exception (similar to else statement). Use only except keyword to define it: Example: This code tries dividing a string by a number, which causes a TypeError.
Published: May 29, 2026
🌐
EyeHunts
tutorial.eyehunts.com › home › python exception stack trace to string
Python exception stack trace to string - Tutorial - By EyeHunts
February 3, 2023 - import traceback try: raise ValueError except ValueError: tb = traceback.format_exc() else: tb = "No error" finally: print(tb) ... Answer: It’s traceback.extract_stack() if you want convenient access to module and function names and line numbers, or ''.join(traceback.format_stack()) if you just want a string that looks like the traceback.print_stack() output. Do comment if you have any doubts or suggestions on this Python stack trace topic. ... All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments.
🌐
Coursera
coursera.org › tutorials › how to catch, raise, and print a python exception
How to Catch, Raise, and Print a Python Exception | Coursera
August 13, 2024 - For example, you can anticipate the errors that may occur and add corresponding except blocks for each one. ... 1 2 3 4 5 6 7 8 9 10 11 12 a = 5 b = "zero" try: quotient = a / b print(quotient) except ZeroDivisionError: print("You cannot divide by zero") except TypeError: print("You must convert strings to floats or integers before dividing") except NameError: print("A variable you're trying to use does not exist")
🌐
Python
docs.python.org › 3 › c-api › exceptions.html
Exception Handling — Python 3.14.7 documentation
On success, this function returns a Python string object with the found line. On failure, this function returns NULL without an exception set. PyObject *PyErr_ProgramText(const char *filename, int lineno)¶ · Part of the Stable ABI. Similar to PyErr_ProgramTextObject(), but filename is a const char*, which is decoded with the filesystem encoding and error handler, instead of a Python object reference.
🌐
Python
docs.python.org › 3.1 › tutorial › errors.html
8. Errors and Exceptions — Python v3.1.5 documentation
December 18, 2020 - TypeError: Can't convert 'int' object to str implicitly · The last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example are ZeroDivisionError, NameError and TypeError. The string printed ...
🌐
Python
python.org › doc › essays › stdexceptions
Standard Exception Classes in Python 1.5 | Python.org
try: raise Exception() except: etype = sys.exc_type # Save it; try-except overwrites it! try: ename = etype.__name__ # Get class name if it is a class except AttributeError: ename = etype print "Sorry:", str(ename) + ":", sys.exc_value Note how this example avoids an explicit type test! Instead, it simply catches the (new) exception raised when the __name__ attribute is not found. Just to be absolutely sure that we're concatenating a string, the built-in function str() is applied.
🌐
Python Course
python-course.eu › python-tutorial › errors-and-exception-handling.php
32. Errors and Exception Handling | Python Tutorial
Traceback (most recent call last): File "C:\\Users\\melis\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py", line 3326, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-15-a5649918d59e>", line 1, in <module> raise SyntaxError("Sorry, my fault!") File "<string>", line unknown SyntaxError: Sorry, my fault! The best or the Pythonic way to do this, consists in defining an exception class which inherits from the Exception class. You will have to go through the chapter on Object Oriented Programming to fully understand the following example:
🌐
GitHub
github.com › PyO3 › pyo3 › issues › 1034
Question: How to convert Python Exception (PyErr) into string for logging · Issue #1034 · PyO3/pyo3
July 11, 2020 - You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... There was an error while loading. Please reload this page. ... I have a python script which raises exception.
Author: PyO3
🌐
seanh.cc
seanh.cc › 2019 › 06 › 20 › python-custom-exception-classes
Designing Python Exception Classes - seanh.cc
June 20, 2019 - >>> e = Exception("Something went wrong") >>> repr(e) >>> "Exception('Something went wrong')" __repr__() is meant to return an information-rich and unambiguous string representation of the object for debugging. Whenever possible it’s supposed to be a valid expression for recreating the object ...
🌐
Acid & Base
sceweb.sce.uhcl.edu › helm › WEBPAGE-Python › documentation › python_tutorial › lib › module-exceptions.html
2.2 Built-in Exceptions
When class exceptions are used, instances of this class have atttributes filename, lineno, offset and text for easier access to the details; for string exceptions, the associated value is usually a tuple of the form (message, (filename, lineno, offset, text)). For class exceptions, str() returns only the message. ... Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. The associated value is a string indicating what went wrong (in low-level terms). You should report this to the author or maintainer of your Python interpreter.