English 中文(简体)
String formatting expressions (Python)
原标题:

String formatting expressions:

 This is %d %s example!  % (1,  nice )

String formatting method calls:

 This is {0} {1} example! .format(1,  nice )

I personally prefer the method calls (second example) for readability but since it is new, there is some chance that one or the other of these may become deprecated over time. Which do you think is less likely to be deprecated?

最佳回答

The original idea was to gradually switch to str.format() approach while allowing both ways:

PEP 3101:
The new system does not collide with any of the method names of the existing string formatting techniques, so both systems can co-exist until it comes time to deprecate the older system.

The idea is still being pursued:

We are still encouraging people to use the new str.format(). Python Issue 7343

Since the original % approach is planned to be deprecated and removed at some point in the future, I would suggest writing new code with str.format(). Though at the moment, it is just a matter of personal preference. I personally prefer using dictionary-based formatting, which is supported by both % operator and str.format() method.

问题回答

Neither; the first one is used in a lot of places and the second one was just introduced. So the question is more which style you prefer. I actually prefer the dict based formatting:

d = {  count : 1,  txt :  nice  }
 This is %(count)d %(txt)s example!  % d

It makes sure that the right parameter goes into the right place, allows to reuse the same parameter in several places, etc.

I thought I read that the % operator is being deprecated in 3.1 already, so I d stick with the format() function.

See PEP 3101: A New Approach To String Formatting





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签