Python Basics: Working with Strings
来源:互联网 发布:淘宝自动装修软件 编辑:程序博客网 时间:2024/05/21 20:28
The format String method
When writing new code, the mechanism of choice is the format string method, which combines and extends the strong points of the earlier methods. Each replacement field is enclosed in curly brackets and may include a name, as well as information on how to convert and format the value supplied for that field.
The simplest case is where the fields have no name, or where each name is just an index.
In [1]:"{}, {} and {}".format("first", "second", "third")
Out[1]: 'first, second and third'The indices need not be in order like this, though.
In [3]:"{3} {0} {2} {1} {3} {0}".format("be","not","or","to")Out[3]: 'to be or not to be'Named fileds work just as expected.
In [4]:from math import piIn [5]"{name} is equal to {value:.2f}".format(name="π",value=pi)Out[5]: 'π is equal to 3.14'The ordering of the keyword arguments does not matter, of course. In this case, I have also supplied a format specifier of .2f, separated from the field name by a colon, meaning we want float-formatting with three decimals. Without the specified, the result would be as follows:
In [5]: "{name} is equal to {value}".format(name="π",value=pi)Out[6]: 'π is equal to 3.141592653589793'Finally, in Python 3.6, there’s a shortcut you can use if you have variables named identically to corresponding replacement fields. In that case, you can use so-called f-strings, written with the prefix f.
In [7]: from math import eIn [8]: f"Euler's constant is equal to {e}"Out[8]: "Euler's constant is equal to 2.718281828459045"Here, the replacement field named e simply extracts the value of the variable of the same name, as the string is being constructed. This is equivalent to the following, slightly more explicit expression:
In [10]: "Euler's constant is roughly {e}.".format(e=e)Out[10]: "Euler's constant is roughly 2.718281828459045."I plan to write some blogs about python and deep learning to record my learning process. I am going back to my dorm, that's all for today.
阅读全文
0 0
- Python Basics: Working with Strings
- Working with Strings
- Chapter 1: Working with strings
- Effective C#-Working with Strings (翻译)
- Effective C#-Working with Strings (翻译)
- windows编程之二 working with strings
- Working with Strings(使用Oracle字符串)
- Python+Basics+With+Numpy+v3
- Working with Generators of Python
- Working with Excel Files in Python
- Python Basics
- Python Basics
- Mining Twitter Data with Python Part 5: Data Visualisation Basics
- Mining Twitter Data with Python Part 6: Sentiment Analysis Basics
- neural networks deep learning Python Basics with numpy (optional) Homework
- 《game design workshop》阅读笔记(三)——Game Design Basics——Working with Formal Elements
- 《game design workshop》阅读笔记(四)——Game Design Basics——Working with Formal Elements
- Web Basics with LWP
- 课后习题page101.pp3.4
- 请详细说下你对vue生命周期的理解?
- Homebrew包管理器的安装与使用
- [高级软件工程实验]内部模块化的命令行菜单小程序V2.0
- C 语言的浮点型问题
- Python Basics: Working with Strings
- PyQt5系列学习笔记之01:通过Qt Creator创建.ui文件,pyuic5翻译.ui文件为.py文件,在PyCharm中调用.py文件
- 承德算命大师解读命与运的奥秘,承德算命大师预测指导
- Python时间差中seconds和total_seconds的区别
- 图像增强技术基础、评价指标及其经典算法
- MyBatis框架理解与使用
- 手游服务端框架之使用Redis实现跨服排行榜
- 【JZOJ5391】卡常题
- 求序列的最大最小值