Is there a brief way in Kotlin to build formatted strings with variables, similar to f-strings in Python? For example:
f'My cat is {weight:.1f} pounds.'
Which would yield:
My cat is 25.3 pounds.
CodePudding user response:
The usual way to do this is with String.format()
.
It's also an extension method on String
, so you can call it as follows:
"My cat is %.1f pounds.".format(weight)
For details, see this question; the docs for the format patterns are here. (Both are for the Java version, but it's the same in Kotlin.)