Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation: f or s

I wonder, is there any difference between these two:

val a = 123
println(f"hello1 $a") // 1                         
println(s"hello1 $a") // 2
like image 414
Incerteza Avatar asked Nov 14 '13 16:11

Incerteza


People also ask

Which character should be used for string interpolation?

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.

Are F-strings faster?

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!

What is the correct syntax for string interpolation?

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.

What is f {} in Python?

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.


1 Answers

According to the docs, f interpolation is typesafe. Also, it allows to add formatting right after the parameter, which s interpolation doesn't support.

like image 142
Ashalynd Avatar answered Sep 28 '22 07:09

Ashalynd