diff --git a/beat/backend/python/baseformat.py b/beat/backend/python/baseformat.py
index 5409c68319a3acda6c2808abfedf07e0c48a605c..83db12059364dfca04bdbde0c246b438b022d395 100644
--- a/beat/backend/python/baseformat.py
+++ b/beat/backend/python/baseformat.py
@@ -267,8 +267,10 @@ def pack_array(dtype, value, fd):
         fd.write(value.tostring())
 
     elif issubclass(dtype, str):  # it is a string
-        for o in value.flat:
-            fd.write(struct.pack(STRING % len(o), len(o), o.encode('utf-8')))
+        for item in value.flat:
+            encoded = item.encode('utf-8')
+            length = len(encoded)
+            fd.write(struct.pack(STRING % length, length, encoded))
 
     else:  # it is a dataformat
         for o in value.flat:
@@ -298,8 +300,9 @@ def pack_scalar(dtype, value, fd):
             fd.write(struct.pack(ENDIANNESS + BINCODE[dtype], value))
 
     elif issubclass(dtype, str):  # it is a string
-        fd.write(struct.pack(STRING % len(value), len(value),
-            value.encode('utf8')))
+        encoded = value.encode('utf-8')
+        length = len(encoded)
+        fd.write(struct.pack(STRING % length, length, encoded))
 
     else:  # it is a dataformat
         value.pack_into(fd)