diff --git a/src/runtime/str.cpp b/src/runtime/str.cpp
index a81c91a3912e6fc9f430cbdee5d346baca135322..02d24d433e273bb490a858c7cf80a5628a58b100 100644
--- a/src/runtime/str.cpp
+++ b/src/runtime/str.cpp
@@ -237,10 +237,14 @@ extern "C" Box* strRepr(BoxedString* self) {
     std::ostringstream os("");
 
     const std::string& s = self->s;
-    os << '\'';
+    char quote = '\'';
+    if (s.find('\'', 0) != std::string::npos && s.find('\"', 0) == std::string::npos) {
+        quote = '\"';
+    }
+    os << quote;
     for (int i = 0; i < s.size(); i++) {
         char c = s[i];
-        if (!_needs_escaping[c & 0xff]) {
+        if ((c == '\'' && quote == '\"') || !_needs_escaping[c & 0xff]) {
             os << c;
         } else {
             char special = 0;
@@ -275,7 +279,7 @@ extern "C" Box* strRepr(BoxedString* self) {
             }
         }
     }
-    os << '\'';
+    os << quote;
 
     return boxString(os.str());
 }
diff --git a/test/tests/repr.py b/test/tests/repr.py
new file mode 100644
index 0000000000000000000000000000000000000000..302d6ff7bca5b25eda8eb10dd31618360485f4b7
--- /dev/null
+++ b/test/tests/repr.py
@@ -0,0 +1,9 @@
+a=1
+try:
+    a.b
+except AttributeError, e:
+    print repr(e)
+
+print repr("both\'\"quotes")
+print repr("single\'quote")
+print repr("double\"quote")