Python Tricks: A Shocking Truth About String Formatting(一)

简介: Python Tricks: A Shocking Truth About String Formatting(一)

Python Tricks: A Shocking Truth About String Formatting
Remember the Zen of Python and how there should be “one obvious way to do something ?” You might scratch your head when you find out that there are four major ways to do string formatting in Python.

Today I’ll demonstrate how these four string formatting approaches work and what their respective strengths and weaknesses are. I’ll also give you my simple “rule of thumb”(经验法则) for how I pick the best general-purpose string formatting approach.

Let’s jump right in, as we’ve got a lot to cover. In order to have a simple toy example for experimentation, let’s assume we’ve got the following variables(or constants, really) to work with:

>>> errno = 50159747054
>>> name = 'Bob'

And based on these variables we’d like to generate an output string with the following error message:

'Hey Bob, there is  a 0xbadc0ffee error!'

Now, that error could really spoil a dev’s Monday morning! But we’re here to discuss string formatting today. So let’s get to work.

#1 - “Old Style” String Formatting
Strings in Python have a unique built-in operation that can be accessed with the %-operator. It’s a shortcut that lets you do simple positional formatting very easily. If you’ve ever worked with a printf-style function in C, you’ll instantly recognize how this works. Here’s a simple example:

>>> 'Hello, %s' % name
'Hello, Bob'

I’m using the %s format specifier here to tell Python where to subsitute the value of name, represented as a string. This is called “old style” string formatting.

In old style string formatting there are also other format specifiers available that let you control the output string. For example, it’s possible to convert numbers to hexadecimal notation or to add whitespace padding to generate nicely formatted tables and reports.

Here, I’m using the %x format specifier to convert an int value to a string and to represent it as a hexadecimal number:

>>> '%x' % errno
'badc0ffee'

The “old style” string formatting syntax changes slightly if you want to make multiple substitutions in a single string. Because the %-operator only takes one argument, you need to wrap the right-hand side in a tuple, like so:

>>> 'Hey %s, there is a 0x%x error!' %(name, errno)
'Hey Bob, there is a 0xbadc0ffee error!'

It’s also possible to refer to variable subsitutions by name in your format string, if you pass a mapping to the %-operator:

>>> 'Hey %(name)s, there is a 0x%(errno)x error!' % {
   "name":name, "errno":errno}'Hey Bob, there is a 0xbadc0ffee error!'

This makes your format strings easier to maintain and easier to modify in the future. You don’t have to worry about making sure the order you’re passing in the values matches up with the order the values are referenced in the format string.

I’m sure you’ve been wondering why this printf-style formatting is called “old style” string formatting. Well, let me tell you. It was technically superseded by “new style” formatting, which we’re going to talk about in a minute. But while “old style” formatting has been deemphasized, it hasn’t been deprecated. It is still supported in the latest version of Python.

#2 - “New Style” String Formatting
Python3 introduced a new way to do string formatting that was also later back-ported to Python2.7. This “new style” string formatting gets rid of the %-operator specifier syntax and makes the syntax for string formatting more regular. Formatting is now handled by calling a format() function on a string object.

You can use the format() function to do simple positional formatting, just like you could with “old style” formatting:

>>> 'Hello, {}'.format(name)
'Hello, Bob'

Or , you can refer to your variable substitutions by name and use them in any order you want. This is quite a powerful feature as it allows for re-arranging the order of display without changing the arguments passed to the format function:

>>> 'Hey {name}, there is a 0x{errno:x} error!'.format(name=name, errno=errno)
'Hey Bob, there is a 0xbadc0ffee error!'

This also shows that the syntax to format an int variable as a hexadecimal string has changed. Now we need to pass a format spec by adding a “:x” suffix after the variable name.

Overall, the format string syntax has become more powerful without complicating the simpler use cases. It pays off to read up on this string formatting mini-language in the Python documentation.

In Python3, this “new style” string formatting is preferred over %-style formatting. However, starting with Python3.6 there’s an even better way to format your strings. I’ll tell you all about it in the next section.

接下文 Python Tricks: A Shocking Truth About String Formatting(二)https://developer.aliyun.com/article/1618444

相关文章
|
1天前
|
Go C# Python
Python Tricks:Python‘s Functions Are First-Class
Python Tricks:Python‘s Functions Are First-Class
19 3
|
1天前
|
安全 JavaScript 前端开发
Python Tricks: A Shocking Truth About String Formatting(二)
Python Tricks: A Shocking Truth About String Formatting(二)
|
1天前
|
Python
Python tricks Context Managers and the with Statement
Python tricks Context Managers and the with Statement
|
1天前
|
开发工具 git Python
Python Tricks : Complacent Comma Placement
Python Tricks : Complacent Comma Placement
|
2月前
|
SQL JSON 测试技术
Python中的f-string
Python中的f-string
|
2月前
|
存储 Serverless 数据处理
Python - len(string)函数
通过上述介绍和示例,我们可以清楚地看到,在Python中,`len()`函数是处理字符串以及其他可迭代对象长度的重要工具。它简单、易用,但在实际应用中却非常强大,无论是在基础编程还是在复杂的数据处理中,`len()`函数都扮演着不可或缺的角色。
54 10
|
3月前
|
Java 开发者 Python
Python中,字符串(String)是一种不可变的数据类型
Python中,字符串(String)是一种不可变的数据类型
|
3月前
|
SQL 数据库 数据安全/隐私保护
【Python】已解决:(SqlServer报错)SQL错误(208):对象名‘string_split’无效
【Python】已解决:(SqlServer报错)SQL错误(208):对象名‘string_split’无效
65 2
|
5月前
|
Python
Python中的f-string记录表达式:调试文档与实践指南
【4月更文挑战第17天】Python 3.8 引入了f-string记录表达式,允许在格式化字符串时执行赋值操作。这在文档字符串和调试时尤其有用。基本语法是 `f"{variable = expression}"`。示例包括在函数文档字符串中展示变量值和在调试输出中记录变量状态。注意性能和可读性,以及赋值顺序。f-string记录表达式提升了代码效率和维护性,成为Python开发的实用工具。
|
1天前
|
iOS开发 MacOS Python
Python 编程案例:谁没交论文?输出并生成电子表格
Python 编程案例:谁没交论文?输出并生成电子表格
17 9