python中格式化字符串

Strings are one of the most essential and used datatypes in programming. It allows the computer to interact and communicate with the world, such as printing instructions or reading input from the user. The ability to manipulate and formate strings give the programmer the power to process and handle text, for example, extract user input from websites forms, data from processed text, and use this data to perform activities such as sentimental analysis. Sentimental analysis is a subfield of natural language processing that allows the computer to classify emotions — negative, positive, or neutral — based on a piece of text.

小号 trings在编程中最重要和使用的数据类型之一。 它允许计算机与世界互动和交流,例如打印指令或从用户那里读取输入。 操纵和组合字符串的能力使程序员能够处理和处理文本,例如,从网站表单中提取用户输入,从处理后的文本中提取数据,并使用该数据执行诸如情感分析之类的活动。 情感分析是自然语言处理的一个子领域,它使计算机可以根据一段文本对情绪(消极,积极或中立)进行分类。

“All sentiment is right; because sentiment has a reference to nothing beyond itself, and is always real, wherever a man is conscious of it.” — David Hume

“所有观点都是正确的; 因为情感无处不在,而无论男人意识到什么,情感总是真实的。” —大卫·休ume

Python is one of the main programming languages used to analyze and manipulate data next to R. Python is used in data analytics to help access databases and communicate with them. It is also useful in importing and exporting data using different web scraping techniques. Once this data is obtained, Python is used to clean and prepare data sets before they are fed to a machine-learning algorithm.

Python是用于分析和处理R之后的数据的主要编程语言之一。Python用于数据分析以帮助访问数据库并与它们进行通信。 在使用不同的Web抓取技术导入和导出数据时,它也很有用。 一旦获得了这些数据,就可以使用Python清理并准备数据集,然后再将其馈送到机器学习算法中。

There are five ways you can use Python to format strings: formatting strings using the modulo operator, formatting using Python’s built-in format function, formatting using Python 3.7 f-strings, formatting manually, and finally using template strings. We will cover each one of these methods as well as when to use each of them.

使用Python格式化字符串的方法有五种:使用运算符格式化字符串,使用Python的内置格式化函数格式化,使用Python 3.7 f字符串格式化, 手动格式化以及最后使用模板字符串。 我们将介绍这些方法中的每一种以及何时使用它们。

Let’s get right to it…

让我们开始吧...

使用模运算符 (Using the modulo operator)

Canva)Canva制作 )

If you wrote code using C or C++ before, this would look very familiar to you. In Python, we can use the modulo operator (%) to perform simple positional formatting. What I call placeHolder here, represents the control specs for formatting and converting the string. The % tells the interpreter that the following characters are the rules for converting the string. The conversion rules include information about the desired width of the result formatted string, the type of the result, and the precision of it — in case of floats.

如果您以前使用C或C ++编写代码,那么您会觉得很熟悉。 在Python中,我们可以使用模运算符(%)进行简单的位置格式设置。 我在这里所谓的placeHolder代表用于格式化和转换字符串的控件规范。 %告诉解释器以下字符是转换字符串的规则。 转换规则包括有关结果格式字符串的所需宽度,结果类型及其精度(如果是浮点数)的信息。

Canva)Canva制作 )

This type of string formatting is sometimes referred to as the “old style” formatting. The placeHolder can refer to values directly or can be specified using keywords arguments.

这种类型的字符串格式有时称为“旧样式”格式。 placeHolder可以直接引用值,也可以使用关键字参数指定。

formatted_str = 'Yesterday I bought %d %s for only %.2f$!' % (10, 2'bananas',2.6743)

If we try to print this string, we will get Yesterday I bought 10 bananas for only 2.67$! .

如果我们尝试打印此字符串,我们将得到Yesterday I bought 10 bananas for only 2.67$!

As you can see here, I had three placeHolders for three values, the first integer, the string — bananas — and the price of the bananas, which is a float. When referring to the float, I decided that a precision of two is all I needed; that’s why only 67 is displayed after the decimal point.

如您在这里看到的,我为三个值设置了三个placeHolders ,它们是第一个整数字符串 -bananas和香蕉的价格,即float 。 当提到浮点数时,我决定只需要2的精度即可。 这就是为什么小数点后仅显示67。

In the previous example, we replaced the placeHolders with the values directly. There is another way we can achieve this mapping, which is through using keyword arguments or by passing a dictionary.

在前面的示例中,我们直接用值替换了placeHolders 。 我们可以通过使用关键字参数或通过传递字典来实现此映射的另一种方法。

name = 'John'balance = 235765formatted_str = 'Hello %(name)s, your current balance is %(balance)6.0f dollars.' % {"name":name, "balance":balance}

Using this method, we need to use the {} instead of the (). Because of that, we can also use a dictionary directly to fill in the placeHolders.

使用此方法,我们需要使用{}而不是() 。 因此,我们还可以直接使用字典来填充placeHolders

dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = 'I got %(quantity)d %(name)s for $%(price).2f$' % dictionary

The complete code for this section:

本节的完整代码:

使用格式化方法 (Using the format method)

Canva)Canva制作 )

In Python 3, a new method to regulate the % operator syntax was introduced to the language. This is done using the built-in format method, also known as the “new style” formatting. Using the format method has many commonalities with using the % operator. Let’s consider rewriting some of the examples from before:

在Python 3中,该语言引入了一种用于调节%运算符语法的新方法。 这是使用内置的格式化方法(也称为“新样式”格式化)完成的。 使用格式方法与使用%运算符有许多共性。 让我们考虑重写之前的一些示例:

name = 'John'balance = 235765formatted_str = 'Hello %(name)s, your current balance is %(balance)6.0f dollars.' % {"name":name, "balance":balance}

Will become:

会变成:

name = 'John'balance = 235765formatted_str = 'Hello {name:s}, your current balance is {balance:6.0f} dollars.'.format("name":name, "balance":balance)

We can summarize the differences between the % operator syntax and the format method as:

我们可以将%运算符语法和format方法的区别总结为:

  1. Using the % operator, we use the () inside the string, while with format method we use {} .

    使用%运算符,我们在字符串内部使用() ,而使用format方法,则使用{}

  2. To define the type of the conversion in the % operator, we set it right after the %, while in the format function we set the conversion after : inside the {}.

    要在%运算符中定义转换的类型,我们将其设置在%之后,而在format函数中,将转换设置在以下位置: {}

However, if we want to pass a dictionary to the format string, we will need to modify the reforestation slightly. Instead of passing the name of the dictionary, we need to pass a pointer to the dictionary, for example:

但是,如果我们想将字典传递给格式字符串,则需要稍作修改。 除了传递字典名称之外,我们还需要传递一个指向字典的指针,例如:

dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = 'I got {quantity:d} {name:s} for {price:.2f}$'.format(**dictionary)

Moreover, we can use the same conversion rules of the % operator that can be used in the format method as well. In Python 3 and up to 3.6, the format method is preferred to the % operator approach. The format method presented a powerful approach to format strings; it didn’t complicate the simple case — such as the examples above — but, at the same time providing new levels to how we format strings.

此外,我们可以使用%运算符的转换规则,该转换规则也可以在format方法中使用。 在Python 3和3.6以下版本中,format方法优于%运算符方法。 格式化方法提供了一种强大的格式化字符串的方法。 它并没有使简单的情况(例如上面的示例)复杂化,但是同时为我们格式化字符串的方式提供了新的层次 。

The complete code for this section:

本节的完整代码:

使用f弦 (Using f-strings)

Canva)Canva制作 )

This, in my opinion, the fanciest and most elegant method to format strings. The f-string was presented in Python 3.8 as a more straightforward approach to string formating. Before we get into f-strings, let’s quickly cover the three types of strings in Python 3.8 and up.

我认为,这是格式化字符串的最简单,最优雅的方法。 f字符串在Python 3.8中提出,是一种更简单的字符串格式化方法。 在介绍f字符串之前,让我们快速介绍一下Python 3.8及更高版本中的三种字符串。

  1. Normal/ standard strings: contained between “” or ‘’, example “Hello, world!”. These are the most commonly used string type.

    普通/标准字符串:包含在“”或“”之间,例如“Hello, world!” 。 这些是最常用的字符串类型。

  2. Raw strings: lead by r”, for example r’hello\nworld’. In this type of strings, no processing is done on the string. Which means, escape characters don't work in these strings. Instead, they will be printed without any processing. This type of strings is mostly used in regular expressions.

    原始字符串 :以r“ r'hello\nworld' ,例如r'hello\nworld' 。 在这种类型的字符串中,不对字符串进行任何处理。 这意味着转义字符在这些字符串中不起作用。 取而代之的是,将不进行任何处理就打印它们。 这种类型的字符串通常用于正则表达式中。

  3. f-strings: lead by f”, for example, f’hello John!’. F-strings as used as an alternative way to format strings.

    f弦:以f f'hello John!' ,例如f'hello John!' 。 F字符串用作格式化字符串的替代方法。

f-strings present a simple way to format strings by directly embedding expression into the string. Let’s revisit the examples from before, one more time.

f字符串提供了一种通过直接将表达式直接嵌入到字符串中来格式化字符串的简单方法。 让我们再回顾一次以前的示例。

name = 'John'balance = 235765formatted_str = 'Hello {name:s}, your current balance is {balance:6.0f} dollars.'.format("name":name, "balance":balance)

If we want to rewrite this formatting using f-strings, it will look like:

如果我们想使用f字符串重写此格式,则它将类似于:

name = 'John'balance = 235765formatted_str = f'Hello {name}, your current balance is {balance} dollars.'

One way f-string simplifies string formatting is, we can directly pass iterables — dictionaries, lists, etc. — to the string, as follows:

f字符串简化字符串格式的一种方法是,我们可以将可迭代对象(字典,列表等)直接传递给字符串,如下所示:

dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = f'I got dictionary[quantity] dictionary[name] for dictionary[price]$'#f-string with listsmyList = [1,2,3,4]formatted_str = f'The last item of the list is myList[-1]'

You can also perform simple arithmetic or logic operations within an f-string directly.

您还可以直接在f字符串中执行简单的算术或逻辑运算。

#ArithmeticmyList = [1,2,3,4]formatted_str = f'The sum of the last and first items of the list is myList[0] + myList[-1]'#Logicformatted_str = f'myList[0] == myList[-1]'

f-strings provide much more freedom to the options we can use inside the string we want to format, such as list indexing, calling list or dictionary methods, or general iterables functions — e.g., len, sum, min, and max. We can also use conditional statements in an f-string:

f字符串为我们可以在要格式化的字符串中使用的选项提供了更大的自由度,例如列表索引,调用列表或字典方法或常规的可迭代函数(例如lensumminmax 。 我们还可以在f字符串中使用条件语句:

age = 21formatted_str = f'You 

The complete code for this section:

本节的完整代码:

手动格式化 (Manual Formatting)

Manual formatting refers to using standard string methods to format strings. Mostly, we use the rjust, ljust, and center to justify the strings. The rjust method right-justifies a string given a specific width by padding it with spaces on the left. The ljust adds padding to the right and the center method adds padding on both sides of the string. These methods do not modify the original string; they instead return a new modified string. There is another string method that can be used to format strings, which is the zfill method. This method pads a string with a numeric value with zeros on the left.

手动格式化是指使用标准字符串方法来格式化字符串。 通常,我们使用rjustljustcenter来对齐字符串。 rjust方法通过在字符串左侧填充空格来对指定宽度的字符串进行右对齐。 ljust会在右边添加填充,而center方法会在字符串的两侧添加填充。 这些方法不修改原始字符串。 相反,它们返回一个新的修改后的字符串。 还有另一种可用于格式化字符串的字符串方法,即zfill方法。 此方法用左边的零填充数字值的字符串。

使用模板字符串 (Using template strings)

Canva)Canva制作 )

Template strings are an alternative to the % operator, but instead of the %, template strings use the $ sign. Both methods provide what we refer to as substitution formatting. Template strings are not built-in in Python, hence to use them, we need to first import the string module. Template strings have three rules:

模板字符串是%运算符的替代方法,但是模板字符串使用$符号代替%。 两种方法都提供了我们称为替代格式的内容。 模板字符串不是Python内置的,因此要使用它们,我们需要首先导入string模块。 模板字符串具有三个规则:

  1. $$ is an escape character.

    $$是转义字符。

  2. Whatever follows the $ sign directly is the placeHolder for the template. By default, these placeHolders are restricted to any case-insensitive alphanumeric string. The first non-placeHolder character after the $ sign terminates this placeHolder specs, for example, a whitespace character.

    直接在$符号后面的是模板的placeHolder 。 默认情况下,这些placeHolders限于任何不区分大小写的字母数字字符串。 $符号后的第一个非placeHolder字符终止了该placeHolder规范,例如,空白字符。

These rules ad more are already defined in the string module, so once imported, you can go ahead and use them without the need to define them.

字符串模块中已经定义了这些规则,因此,一旦导入,您就可以继续使用它们而无需定义它们。

The template string has two main methods: the substitute method and the safe_substitute method. Both methods take the same argument, which is the substitute mapping. Moreover, both methods return a new formatted string, you can think of the template object as the blueprint for how future strings will be formatted. For example:

模板字符串有两个主要方法: substitute方法和safe_substitute方法。 两种方法都采用相同的参数,即替代映射。 而且,这两种方法都返回一个新的格式化字符串,您可以将模板对象视为将来如何格式化字符串的蓝图。 例如:

from string import Templateformatted_str = Template('Hello $who, your current balance is $how dollars.')formatted_str.substitute(who='John', how=235765)

To see the difference between the substitute and the safe_substitute methods, let’s try using a dictionary as our substitution mapping.

要查看替代方法和safe_substititute方法之间的区别,让我们尝试使用字典作为替代映射。

from string import Templatemapping = dict(who='John', how=235765)formatted_str = Template('Hello $who, your current balance is $how dollars.')Returned_str = formatted_str.substitute(mapping)

Assume our dictionary doesn’t have all the keys we need for the mapping, using the substitute method will give a keyError. Here's where safe_substitute comes to the help, if this method didn't find the correct key, it will return a string with the placeHolder for the wrong key. For example, this code:

假设我们的字典没有我们映射所需的所有键,那么使用substitute方法将给出一个keyError 。 这是safe_substitute提供帮助的地方,如果此方法找不到正确的密钥,它将为错误的密钥返回一个带有placeHolder的字符串。 例如,此代码:

from string import Templatemapping = dict(who='John')formatted_str = Template('Hello $who, your current balance is $how dollars.')Returned_str = formatted_str.safe_substitute(mapping)

Will return Hello John, your current balance is $how dollars. where if I had used the substitute method, I would've gotten an error.

将返回Hello John, your current balance is $how dollars. 如果我使用了substitute方法,那我会得到一个错误。

The complete code for this section:

本节的完整代码:

结论 (Conclusion)

In this article, we covered five different ways you can use to format a string in Python:

在本文中,我们介绍了可用于在Python中格式化字符串的五种不同方式:

  1. The modulo operator.
    模运算符。
  2. The format function.
    格式功能。
  3. f-strings.
    F弦。
  4. Manual formatting.
    手动格式化。
  5. Template strings.
    模板字符串。

Each method has its usage advantages and disadvantages. A general rule of thumb, as put by Dan Badar, if the string you’re formatting is user-obtained, template strings are the way to go. If not, use f-strings if you’re on Python 3.7 or the format function elsewhere.

每种方法都有其使用的优缺点。 根据Dan Badar的一般经验法则,如果要格式化的字符串是用户获得的,则使用模板字符串是可行的方法。 否则,如果您使用的是Python 3.7或其他地方的format函数,请使用f字符串。

翻译自: https://towardsdatascience.com/a-guide-to-everything-string-formatting-in-python-e724f101eac5

python中格式化字符串

http://www.taodudu.cc/news/show-995073.html

相关文章:

  • 机器学习 缺陷检测_球检测-体育中的机器学习。
  • 先知模型 facebook_使用Facebook先知进行犯罪率预测
  • 鲜为人知的6个黑科技网站_6种鲜为人知的熊猫绘图工具
  • 数据eda_关于分类和有序数据的EDA
  • 优化 回归_使用回归优化产品价格
  • 大数据数据科学家常用面试题_进行数据科学工作面试
  • vue.js python_使用Python和Vue.js自动化报告过程
  • 计算机科学必读书籍_5篇关于数据科学家的产品分类必读文章
  • python 网页编程_通过Python编程检索网页
  • data studio_面向营销人员的Data Studio —报表指南
  • 乐高ev3 读取外部数据_数据就是新乐高
  • java 分裂数字_分裂的补充:超越数字,打印物理可视化
  • 比赛,幸福度_幸福与生活满意度
  • 5分钟内完成胸部CT扫描机器学习
  • openai-gpt_为什么到处都看到GPT-3?
  • 数据可视化及其重要性:Python
  • ai驱动数据安全治理_AI驱动的Web数据收集解决方案的新起点
  • 使用K-Means对美因河畔法兰克福的社区进行聚类
  • 因果关系和相关关系 大数据_数据科学中的相关性与因果关系
  • 分类结果可视化python_可视化分类结果的另一种方法
  • rstudio 管道符号_R中的管道指南
  • 时间序列因果关系_分析具有因果关系的时间序列干预:货币波动
  • 无法从套接字中获取更多数据_数据科学中应引起更多关注的一个组成部分
  • 深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景
  • 数据中台是下一代大数据_全栈数据科学:下一代数据科学家群体
  • 泰坦尼克数据集预测分析_探索性数据分析-泰坦尼克号数据集案例研究(第二部分)
  • 大数据技术 学习之旅_如何开始您的数据科学之旅?
  • 搜索引擎优化学习原理_如何使用数据科学原理来改善您的搜索引擎优化工作
  • 一件登录facebook_我从Facebook的R教学中学到的6件事
  • python 图表_使用Streamlit-Python将动画图表添加到仪表板

python中格式化字符串_Python中所有字符串格式化的指南相关推荐

  1. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  2. python中定义数据结构_Python中的数据结构—简介

    python中定义数据结构 You have multiples algorithms, the steps of which require fetching the smallest value ...

  3. python中定义字符串_python中的字符串

    python中的字符串一旦定义,则不可以修改 python中的原始字符串 即 在字符串前面加小写字母r 比如:打印输出C:\Program Files\Microsoft Games python中的 ...

  4. python中查找字符串_python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  5. python语言字符串_python中字符串的常见操作方法

    原博文 2019-09-06 09:49 − 1. 字符串概念,字符串是一个容器,包含若干个字符并按照一定的顺序组织成一个整体.字符串支持索引操作. 2. 创建字符串基本语法 变量名 = " ...

  6. python数字转字符串_python中如何将数字转字符串

    python数字转字符串的实现方法:1.使用格式化字符串,语句如"tt=322 tem='%d' %tt":2.通过"str(5)"方法实现转换. 推荐:< ...

  7. python数据处理常用函数_Python中常用操作字符串的函数与方法总结

    Python中常用操作字符串的函数与方法总结 这篇文章主要介绍了Python中常用操作字符串的函数与方法总结,包括字符串的格式化输出与拼接等基础知识,需要的朋友可以参考下 例如这样一个字符串 Pyth ...

  8. python输出数字和字符串_Python中的数值和字符串

    一.Python中的数值 python中的数值分为三类:整型.浮点型和复数 1.整型: python3的整型支持各种类型的整数: python的整数值有4种表达形式: a.十进制形式,最普通的整数就是 ...

  9. python判断字符串中包含某个字符串_Python中最常用的字符串方法!

    译 / 忧郁的红秋裤 字符串是字符序列.Python中内置的string类代表基于Unicode国际字符集的字符串.除了Python中常见的操作外,字符串还有一些专属于它们的附加方法.下图显示了所有这 ...

最新文章

  1. JQuery Datatable用法
  2. Tensorflow MNIST浅层神经网络的解释和答复
  3. QEMU虚拟机关键源代码学习
  4. 文件搜索工具everything
  5. Windows下编译和安装Boost库
  6. 两个不同分支之间的代码合并
  7. sdr 软件_无线电爱好者系列-1.通过SDR获取周围无线电信号及应用
  8. 《xUnit Test Patterns》学习笔记2 - Goal Of Test Automation
  9. 如何实施域控制器活动目录备份与恢复
  10. Matlab意识流速成班
  11. Oracle 11.2.0.2 Patch 说明
  12. java 线程 数量_Java线程数量随时间增长的原因
  13. 学生选课系统软件测试报告,学生选课系统-软件可用性测试实验
  14. 作为刚出培训机构的面试之路
  15. 八大排序算法--堆排序
  16. 小波学习笔记——MATLAB
  17. oracle purge作业,Oracle purge用法介绍
  18. HTML 元素学习指南
  19. 365天挑战LeetCode1000题——Day 126 单调栈模板 500题纪念
  20. 【问】登陆提示用户SA登陆失败

热门文章

  1. 冲突域 广播域简单解释
  2. php 常用正则运算
  3. Android Studio 导入新工程项目
  4. overlay 如何实现跨主机通信?- 每天5分钟玩转 Docker 容器技术(52)
  5. 代码能不能不要写得这么烂?!
  6. Kubectl指令集
  7. UDP打洞NAT大致分为下面四类 P2P
  8. WebM VP8 SDK Usage/关于WebM VP8 SDK的用法
  9. 网站漏洞检测针对区块链网站安全分析
  10. Nutanix公布财报数据 准备IPO前表现抢眼