format is a new method for formatting strings in python2.6. Compared with the old version of the % format method, it has many advantages.
1. There is no need to worry about the data type. In the % method, %s can only replace the string type (recommended learning: Python video tutorial)
2. A single parameter can be output multiple times, and the order of parameters can be different
3. The filling method is very flexible and the alignment is very powerful
4. The officially recommended method, the % method will be eliminated in later versions
An example of format
print 'hello {0}'.format('world')
Output:
hello world
Specific use case:
#通过位置 print '{0},{1}'.format('chuhao',20) print '{},{}'.format('chuhao',20) print '{1},{0},{1}'.format('chuhao',20) #通过关键字参数 print '{name},{age}'.format(age=18,name='chuhao') class Person: def __init__(self,name,age): self.name = name self.age = age def __str__(self): return 'This guy is {self.name},is {self.age} old'.format(self=self) print str(Person('chuhao',18)) #通过映射 list a_list = ['chuhao',20,'china'] print 'my name is {0[0]},from {0[2]},age is {0[1]}'.format(a_list) #my name is chuhao,from china,age is 20 #通过映射 dict b_dict = {'name':'chuhao','age':20,'province':'shanxi'} print 'my name is {name}, age is {age},from {province}'.format(**b_dict) #my name is chuhao, age is 20,from shanxi #填充与对齐 print '{:>8}'.format('189') # 189 print '{:0>8}'.format('189') #00000189 print '{:a>8}'.format('189') #aaaaa189 #精度与类型f #保留两位小数 print '{:.2f}'.format(321.33345) #321.33 #用来做金额的千位分隔符 print '{:,}'.format(1234567890) #1,234,567,890 #其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。 print '{:b}'.format(18) #二进制 10010 print '{:d}'.format(18) #十进制 18 print '{:o}'.format(18) #八进制 22 print '{:x}'.format(18) #十六进制12
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of What does format mean in python?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
