参考链接: 从Java中的字符串中删除前导零

字符串太长 pep8

Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498. It’s also called literal string interpolation.

Python f字符串或格式化的字符串是格式化字符串的新方法。 此功能是在PEP-498下的Python 3.6中引入的。 也称为文字字符串插值 。

为什么我们需要F弦? (Why do we need f-strings?)

Python provides various ways to format a string. Let’s quickly look at them and what are the issues they have.

Python提供了多种格式化字符串的方法。 让我们快速查看它们以及它们有什么问题。

% formatting – great for simple formatting but limited support for strings, ints, doubles only. We can’t use it with objects. %格式 -非常适合简单的格式设置,但仅对string ,ints和double的支持有限。 我们不能将其与对象一起使用。 Template Strings – it’s very basic. Template strings work with keyword arguments like dictionary only. We are not allowed to call any function and arguments must be strings. 模板字符串 –非常基本。 模板字符串仅可与关键字参数(例如字典)一起使用。 我们不允许调用任何函数,并且参数必须是字符串。 String format() – Python String format() function was introduced to overcome the issues and limited features of %-formatting and template strings. However, it’s too verbose. Let’s look at its verbosity with a simple example.>>> age = 4 * 10

>>> 'My age is {age}.'.format(age=age)

'My age is 40.' 字符串格式()– Python字符串格式()函数的引入是为了克服%格式和模板字符串的问题和有限的功能。 但是,它太冗长了。 让我们用一个简单的例子来看看它的详细程度。

Python f-strings works almost similar like format() function but removes all the verbosity that format() function has. Let’s see how easily we can format the above string using f-strings.

Python f字符串的工作原理几乎类似于format()函数,但删除了format()函数具有的所有冗长性。 让我们看看使用f字符串格式化上述字符串的难易程度。

>>> f'My age is {age}'

'My age is 40.'

Python f-strings is introduced to have minimal syntax for string formatting. The expressions are evaluated at runtime. If you are using Python 3.6 or higher version, you should use f-strings for all your string formatting requirements.

引入Python f字符串是为了使字符串格式化的语法最少 。 在运行时对表达式求值。 如果您使用的是Python 3.6或更高版本,则应使用f-strings满足所有字符串格式要求。

Python f字符串示例 (Python f-strings examples)

Let’s look at a simple example of f-strings.

让我们看一个简单的f字符串示例。

name = 'Pankaj'

age = 34

f_string = f'My Name is {name} and my age is {age}'

print(f_string)

print(F'My Name is {name} and my age is {age}')  # f and F are same

name = 'David'

age = 40

# f_string is already evaluated and won't change now

print(f_string)

Output:

输出:

My Name is Pankaj and my age is 34

My Name is Pankaj and my age is 34

My Name is Pankaj and my age is 34

Python executes statements one by one and once f-string expressions are evaluated, they don’t change even if the expression value changes. That’s why in the above code snippets, f_string value remains same even after ‘name’ and ‘age’ variable has changed in the latter part of the program.

Python会一一执行语句,并且一旦评估了f字符串表达式,即使表达式值更改,它们也不会更改。 这就是为什么在上面的代码段中,即使在程序的后半部分更改了“ name”和“ age”变量后,f_string值仍保持不变。

1.带表达式和转换的f字符串 (1. f-strings with expressions and conversions)

We can use f-strings to convert datetime to a specific format. We can also run mathematical expressions in f-strings.

我们可以使用f字符串将日期时间转换为特定格式。 我们还可以在f字符串中运行数学表达式。

from datetime import datetime

name = 'David'

age = 40

d = datetime.now()

print(f'Age after five years will be {age+5}')  # age = 40

print(f'Name with quotes = {name!r}')  # name = David

print(f'Default Formatted Date = {d}')

print(f'Custom Formatted Date = {d:%m/%d/%Y}')

Output:

输出:

Age after five years will be 45

Name with quotes = 'David'

Default Formatted Date = 2018-10-10 11:47:12.818831

Custom Formatted Date = 10/10/2018

2. f字符串支持原始字符串 (2. f-strings support raw strings)

We can create raw strings using f-strings too.

我们也可以使用f字符串创建原始字符串。

print(f'Default Formatted Date:\n{d}')

print(fr'Default Formatted Date:\n {d}')

Output:

输出:

Default Formatted Date:

2018-10-10 11:47:12.818831

Default Formatted Date:\n 2018-10-10 11:47:12.818831

3.具有对象和属性的f字符串 (3. f-strings with objects and attributes)

We can access object attributes too in f-strings.

我们也可以在f字符串中访问对象属性。

class Employee:

id = 0

name = ''

def __init__(self, i, n):

self.id = i

self.name = n

def __str__(self):

return f'E[id={self.id}, name={self.name}]'

emp = Employee(10, 'Pankaj')

print(emp)

print(f'Employee: {emp}\nName is {emp.name} and id is {emp.id}')

Output:

输出:

E[id=10, name=Pankaj]

Employee: E[id=10, name=Pankaj]

Name is Pankaj and id is 10

4. f字符串调用函数 (4. f-strings calling functions)

We can call functions in f-strings formatting too.

我们也可以用f字符串格式调用函数。

def add(x, y):

return x + y

print(f'Sum(10,20) = {add(10, 20)}')

Output: Sum(10,20) = 30

输出: Sum(10,20) = 30

5.带空格的f字符串 (5. f-string with whitespaces)

If there are leading or trailing whitespaces in the expression, they are ignored. If the literal string part contains whitespaces then they are preserved.

如果表达式中存在前导或尾随空格,则将其忽略。 如果文字字符串部分包含空格,则将保留它们。

>>> age = 4 * 20

>>> f'   Age = {  age   }  '

'   Age = 80  '

6.带f字符串的Lambda表达式 (6. Lambda expressions with f-strings)

We can use lambda expressions insidef-string expressions too.

我们也可以在字符串表达式内部使用lambda表达式 。

x = -20.45

print(f'Lambda Example: {(lambda x: abs(x)) (x)}')

print(f'Lambda Square Example: {(lambda x: pow(x, 2)) (5)}')

Output:

输出:

Lambda Example: 20.45

Lambda Square Example: 25

7. f弦的其他示例 (7. f-strings miscellaneous examples)

Let’s look at some miscellaneous examples of Python f-strings.

让我们看一些Python f字符串的其他示例。

print(f'{"quoted string"}')

print(f'{{ {4*10} }}')

print(f'{{{4*10}}}')

Output:

输出:

quoted string

{ 40 }

{40}

That’s all for python formatted strings aka f-strings.

这就是python格式的字符串,又称f字符串。

GitHub Repository.

GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: PEP-498, Official Documentation

参考: PEP-498 , 官方文档

翻译自: https://www.journaldev.com/23592/python-f-strings-literal-string-interpolation

字符串太长 pep8

[转载] 字符串太长 pep8_Python f字符串– PEP 498 –文字字符串插值相关推荐

  1. 字符串太长 pep8_Python f字符串– PEP 498 –文字字符串插值

    字符串太长 pep8 Python f-strings or formatted strings are the new way to format strings. This feature was ...

  2. oracle字段大段字符串,ORACLE存储过程,参数目字符串太长

    ORACLE存储过程,参数字符串太长 在做存储过程开发的时候,需要拼SQL语句,比如 DECLARE V_SQL VARCHAR2(32000); BEGIN V_SQL := 'INSERT INT ...

  3. php 长文本_php字符串太长怎么办

    php字符串太长的解决办法:首先截取长度等于0或大于等于本字符串的长度,则返回字符串本身:然后如果截取长度为负数,那么截取长度就等于字符串长度减去截取长度:最后如果截取长度的绝对值大于字符串本身长度, ...

  4. web前端工作笔记008---jQuery table jstable的使用方法,字符串太长显示...初始化显示数据

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 刷新表格: 下面这样刷新,需要这两行 var pages = _table.page.info() ...

  5. HttpClient FormUrlEncodedContent System.UriFormatException: 无效的 URI: URI 字符串太长问题解决方案

    HttpClient FormUrlEncodedContent System.UriFormatException: 无效的 URI: URI 字符串太长问题解决方案 参考文章: (1)HttpCl ...

  6. python语句如何换行和字符串太长如何换行

    python语句如何换行和字符串太长如何换行 python语句如何换行 建议每行代码的长度不要超过80个字符.对于过长的代码,建议进行换行. 在需要换行处(可使用空格的地方)使用反斜杠\ 实现换行,其 ...

  7. python字符串换行连接_python入门 python字符串换行显示、字符串太长\连接多行

    #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 缩进 换行 """ &qu ...

  8. 【C语言】字符串太长换行连接

    '\' 也叫宏延续运算符 字符串太长时换行: char* A = "AAA\BBB\n";char* B = "AAA"\"BBB\n";p ...

  9. ORA-06550 PLS-00172 字符串太长 超过3W 处理方式

    问题 描述 帖子 通过sql插入clob数据(数据库对应字段类型为clob), 在执行insert语句时(待插入的字符串有2W多), 总是报错如下: 不是说clob支持4G大小么? 有些资料说是sql ...

最新文章

  1. pytorch 多GPU训练总结(DataParallel的使用)
  2. ruby on rails_我成为了Ruby on Rails和React的贡献者,你也可以
  3. LJL-Solution-vss2005在项目中连接不上 解决方案
  4. CentOS 7 源码编译安装 Redis
  5. php json转数组后并在前端展示,0516-如何从服务器端获取JSON格式字符串并解决到前端页面中显示...
  6. ABAP:为Table Control创建Context Menu
  7. 微服务发现组件Eureka:简介以及Eureka服务端开发
  8. ActionT 委托
  9. Android 下载的文件进行MD5校验
  10. 2.5寸移动硬盘盒改装 驱动3.5寸台式机硬盘
  11. 一款界面友好的思维导图软件MindMaster
  12. C#SharpDevelop如何开发插件详细记录
  13. Python数据处理性能对比,原生,Pandas,Numpy哪个更优秀
  14. EFI和Legacy启动方式
  15. 2017目标跟踪算法综述
  16. 《中国聚合支付行业发展报告2018》发布 深度分析未来八大趋势
  17. B站【狂神说Java笔记】-注解和反射
  18. 推荐一个批量改文件名的工具
  19. 根据产品损耗率计算产品实际数量
  20. win10去除右键菜单中的“使用skype共享”

热门文章

  1. 发布 项目_项目发布会活动到底应该怎么办
  2. gcc -strip编译选项的作用
  3. 下列关于html5表单的多样输入方式,IT兄弟连 HTML5教程 HTML5表单 多样的输入类型1...
  4. 图解自监督学习(CV)
  5. python3.8使用方法_python3.8新特性
  6. 2048游戏c语言linux简易代码,C语言实现2048游戏代码
  7. python 怎么调用 矩阵 第几行_第58集 python机器学习:混淆矩阵精度指标
  8. python默认深拷贝_Python 深拷贝与浅拷贝
  9. python 小说爬虫_从零开始写Python爬虫 --- 1.7 爬虫实践: 排行榜小说批量下载
  10. java 监听窗口是否改变_JAVA项目监听文件是否发生变化