Name = "{{{}}}" Name. Format("something") Why the answer is {something} ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Name = "{{{}}}" Name. Format("something") Why the answer is {something} ?

13th Jan 2021, 4:59 PM
Gautam Yadav
Gautam Yadav - avatar
8 Answers
+ 3
'{{' is like an escape for '{'. When you use '{' or '}' in a string, Python thinks that it has to be replaced with a format argument. Therefore to use '{' or '}' in a string, you need to use '{{' or '}}' respectively. In you example, the first 2 '{' are considered to be 1 '{' and the last 2 '}}'nare considered to be 1 '}' and the '{}' in the centre is replaced by the argument.
13th Jan 2021, 5:14 PM
XXX
XXX - avatar
+ 2
Gautam Yadav it is because you are not passing format arguments to it. '{{' is considered as '{' only when the string is either a f-string or .format() is called on the string. Else the string will stay as it is.
13th Jan 2021, 5:42 PM
XXX
XXX - avatar
+ 1
XXX then why it's not the same as you said when I do something like this name = "{{{something}}}" ? Answer : '{{{something}}}'
13th Jan 2021, 5:34 PM
Gautam Yadav
Gautam Yadav - avatar
+ 1
Ok XXX
13th Jan 2021, 5:55 PM
Gautam Yadav
Gautam Yadav - avatar
0
What do you want to get as a result?
13th Jan 2021, 5:08 PM
Bánk Kozma
Bánk Kozma - avatar
0
Bánk Kozma 'something' may be Or an error
13th Jan 2021, 5:11 PM
Gautam Yadav
Gautam Yadav - avatar
0
Bánk Kozma but not that!
13th Jan 2021, 5:11 PM
Gautam Yadav
Gautam Yadav - avatar
0
XXX thanks you are sweet :)
13th Jan 2021, 5:45 PM
Gautam Yadav
Gautam Yadav - avatar