diff --git a/python/taichi/lang/impl.py b/python/taichi/lang/impl.py index 5f40367b238e6..0ebc14f07781d 100644 --- a/python/taichi/lang/impl.py +++ b/python/taichi/lang/impl.py @@ -701,10 +701,11 @@ def fused_string(entries): @taichi_scope -def ti_format(*args): +def ti_format(*args, **kwargs): content = args[0] mixed = args[1:] new_mixed = [] + new_mixed_kwargs = {} args = [] for x in mixed: if isinstance(x, ti.Expr): @@ -712,9 +713,14 @@ def ti_format(*args): args.append(x) else: new_mixed.append(x) - + for k, v in kwargs.items(): + if isinstance(v, ti.Expr): + new_mixed_kwargs[k] = '{}' + args.append(v) + else: + new_mixed_kwargs[k] = v try: - content = content.format(*new_mixed) + content = content.format(*new_mixed, **new_mixed_kwargs) except ValueError: print('Number formatting is not supported with Taichi fields') exit(1) diff --git a/tests/python/test_print.py b/tests/python/test_print.py index 1442fa76108f3..bfed98e9af398 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -126,3 +126,19 @@ def test_python_scope_print_field(): print(x) print(y) print(z) + + +@ti.test(arch=ti.cpu) +def test_print_string_format(): + @ti.kernel + def func(k: ti.f32): + print(123) + print("{} abc".format(123)) + print("{} {} {}".format(1, 2, 3)) + print("{} {name} {value}".format(k, name=999, value=123)) + name = 123.4 + value = 456.7 + print("{} {name} {value}".format(k, name=name, value=value)) + + func(233.3) + ti.sync()