Python
TensorFlow
String Manipulation
File Path
Programming

Joining string and tf.string to get a path

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Joining a normal Python string with a tf.string tensor is easy to get wrong because the two values live in different execution worlds. Python string operations happen immediately in Python, while tf.string operations are TensorFlow ops that become part of a graph or input pipeline. The right solution depends on whether your path is being built in normal Python code or inside TensorFlow execution.

When Plain Python Path Joining Is Correct

If both values are ordinary Python strings, use normal path tools such as pathlib or os.path.join.

python
1from pathlib import Path
2
3base_dir = "/tmp/data"
4filename = "image-001.jpg"
5path = Path(base_dir) / filename
6print(path)

This is the best approach when you are preparing paths before building a TensorFlow dataset or when the values never become tensors.

Why Python + Does Not Solve TensorFlow String Tensors

A tf.string tensor is not the same thing as a Python str. If filename is a tensor, Python path tools cannot combine it the way they combine normal strings.

Example tensor value:

python
1import tensorflow as tf
2
3filename = tf.constant("image-001.jpg")
4print(filename)

At this point, you need TensorFlow string operations, not ordinary Python concatenation helpers.

Use tf.strings.join Inside TensorFlow Code

When at least one component is a tf.string, use tf.strings.join.

python
1import tensorflow as tf
2
3base_dir = tf.constant("/tmp/data")
4filename = tf.constant("image-001.jpg")
5full_path = tf.strings.join([base_dir, filename], separator="/")
6
7print(full_path.numpy().decode())

This is the TensorFlow-native way to build a path-like string in eager execution or inside a dataset map function.

Mixing Python str and tf.string

You do not have to convert everything manually to tensors first. TensorFlow will happily accept a Python string literal in many string ops, but the result is still a tensor.

python
1import tensorflow as tf
2
3base_dir = "/tmp/data"
4filename = tf.constant("image-001.jpg")
5full_path = tf.strings.join([base_dir, filename], separator="/")
6
7print(full_path)

This is often the simplest answer when one side is static and the other comes from a TensorFlow pipeline.

Practical Example in a tf.data Pipeline

This is where the distinction really matters. Suppose you have dataset elements containing filenames and you want to build full image paths before reading files.

python
1import tensorflow as tf
2
3base_dir = "/tmp/data"
4filenames = tf.data.Dataset.from_tensor_slices(["a.jpg", "b.jpg"])
5
6
7def make_path(name):
8    return tf.strings.join([base_dir, name], separator="/")
9
10
11paths = filenames.map(make_path)
12for item in paths:
13    print(item.numpy().decode())

Using os.path.join inside map would not be the right tool here because the dataset element name is a tensor, not a Python string.

Be Careful About Path Separators

For local filesystem paths in pure Python, pathlib is the best cross-platform option. Inside TensorFlow string ops, developers often hardcode "/" because TensorFlow file APIs and many ML workflows use forward-slash paths consistently.

If you are building paths that must match a platform-specific convention outside TensorFlow file handling, do the join in Python before converting to tensors. That avoids mixing OS-specific path semantics with graph string operations.

When to Convert Back to Python Strings

If you only need the final path in Python code, convert it back after TensorFlow creates it:

python
1import tensorflow as tf
2
3value = tf.strings.join(["/tmp/data", tf.constant("a.jpg")], separator="/")
4python_path = value.numpy().decode("utf-8")
5print(python_path)

This only works in eager execution. Inside a graph or exported function, keep the value as a tensor until it reaches the next TensorFlow op.

Common Pitfalls

  • Using os.path.join on a tf.string tensor inside a TensorFlow pipeline.
  • Treating tf.string as if it were a normal Python str.
  • Building paths with Python string concatenation inside tf.data mapping functions.
  • Converting tensors back to Python strings too early and breaking graph-friendly execution.
  • Forgetting that the result of tf.strings.join is still a tensor, not a Python path object.

Summary

  • Use Python path tools when all parts are ordinary strings.
  • Use tf.strings.join when any path component is a tf.string tensor.
  • In tf.data pipelines, keep path creation inside TensorFlow string ops.
  • Convert back to a Python string only when the value leaves TensorFlow execution.
  • The main design question is not syntax, but whether the path is being built in Python or inside the TensorFlow graph.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.