Home > Software design >  Could anyone explain the print result in Python(sorry my English is poor,so please forgive me for un
Could anyone explain the print result in Python(sorry my English is poor,so please forgive me for un

Time:07-27

The code are as follows:

str3 = "{{0}}"
print(str3.format("Don't print"))

And the result is:

{0}

I don't know why the result is not :

"Don't print"

CodePudding user response:

Do you want Don't print or "Don't print" if it's the first then just with str3 = "{0}" will work

str3 = "{0}"
print(str3.format("Don't print"))

if is the second way you could use

print(str3.format("\"Don't print\""))

CodePudding user response:

To get the proper .format() result you are looking for, you should use only one set of curly braces. It would look like this:

myStr = "{0}"
print(myStr.format("Don't print"))

CodePudding user response:

The .format command does not work this way. I suggest you read up on this. w3schools has a good explanation/tutorial on how to use the syntax:

https://www.w3schools.com/python/ref_string_format.asp

Hope this helps.

  • Related