I wonder, is there any difference between these two:
val a = 123
println(f"hello1 $a") // 1
println(s"hello1 $a") // 2
To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the $ and the " that starts a string literal.
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.
The f or F in front of strings tells Python to look at the values inside {} and substitute them with the values of the variables if exist.
According to the docs, f
interpolation is typesafe. Also, it allows to add formatting right after the parameter, which s
interpolation doesn't support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With