python - Are there other ways to format strings other then comma, percent, plus sign? -


I see and I can not find a definite answer to this question: print variable in Python?

By now, I have seen three ways: using the works, using percentages, or using the plus symptoms:

  gt ; & Gt; A = "Hello"> gt; & Gt; & Gt; B = "world"> gt; & Gt; & Gt; Print, "to the", b Hello for the world & gt; & Gt; & Gt; Print "% s to% s"% (a, b) Hello to the world & gt; & Gt; & Gt;   

Each method has its pros and cons.

Comos have allowed directly to write variables and add spaces, and simultaneously perform a string conversion if needed, but I remember that good coding practices say that It is best to separate your variable from your text.

Percentages are permitted, although they need to use a list when more than one variable, and you have to type the variable type (although it seems able to convert, Even if it is not a variable type, like trying to print a number with% s).

plus sign "worst" as they mix variables and text, and do not convert on the fly; Although it may be necessary to have more control over your variation from time to time.

I have looked around and it seems that some of these methods can now be obsolete. Since all of them work and everyone has their professional and opposition, I am thinking: Is there a recommended method, or do they depend on all references?

string.format () Basics

Here are some examples of original string replacements , {} Placeholder If there is no format specified for the replacement variable, then it will be inserted as a string and will format.

  s1 = "much depends on {} .format ("rain", "white") water   

The format ("a red wheel barrow") s2 = "shiny {} {beside the chickens}". You can also use the numerical position of the variable and convert them into wires, this gives some flexibility when making a formatting, if you have a mistake in ordering around all the variables without modification you can easily correct. .format ("Emacs", "vim") s2 = .format ("Emacs", "vim")

  S1 = "better than {0} {1} code>  

format () function "better than {0}" provides a reasonable amount of additional features and capabilities, some useful tips here You can use a new templating engine and logic, rather than a strict command of

named logic

The requirement is to be named after the use of string format. (Function = "service is okay", object = "cheese", location = "table"}).

  madlib = "i {ver} {} {object place} closed" gt; & Gt; & Gt; I took cheese from the table   

Reuse several times many times

To use the% formatter, a strict order of variable , .format () method allows you to put them in any order, as we saw above in basic topics, but also allow for re-use.

  str = "oh {0}, {0}! So art thou {0}?" format ("Romeo") & gt; & Gt; & Gt; Oh Romeo, Romeo! Wherefore art thou Romeo?   

Use the format as a function

You can use the format as a function, which is a bit of code Separately allows text and formatting. For example, at the beginning of your program, you can include all your formats and then use them later. This can be a good way to handle internationalization, in which not only requires separate lessons, but often numbers require different formats.

  email_f = "Your email address was {email}". (Email = "bob@example.com"))   

Avoiding braces

If you use braces while using str.format Is used to (), just double up

  print ("{} set is often represented as {{}}." Format ("blank")> gt ; & Gt; & Gt; The empty set is often shown as {0}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -