Python's many ways of string formatting — are the older ones (going to be) deprecated?

前端 未结 5 1916
自闭症患者
自闭症患者 2020-11-22 07:01

Python has at least six ways of formatting a string:

In [1]: world = \"Earth\"

# method 1a
In [2]: \"Hello, %s\" % world
Out[2]: \'Hello, Earth\'

# method          


        
5条回答
  •  Happy的楠姐
    2020-11-22 07:14

    Looking at the older Python docs and PEP 3101 there was a statement that the % operator will be deprecated and removed from the language in the future. The following statement was in the Python docs for Python 3.0, 3.1, and 3.2:

    Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.

    If you go to the same section in Python 3.3 and 3.4 docs, you will see that statement has been removed. I also cannot find any other statement anywhere else in the documentation indicating that the operator will be deprecated or removed from the language. It's also important to note that PEP3101 has not been modified in over two and a half years (Fri, 30 Sep 2011).

    Update

    PEP461 Adding % formatting to bytes and bytearray is accepted and should be part of Python 3.5 or 3.6. It's another sign that the % operator is alive and kicking.

提交回复
热议问题