综合编程

Format Strings in Python

微信扫一扫,分享到朋友圈

Format Strings in Python
0

We have very powerful method str.format() in python through which you can do variable substitutions and value formatting.

Let’s understand the usage of format with couple of examples.

Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and calling the str.format(). The value we want to put into the placeholders and concatenate with the string passed as parameters into the format function.

format() method takes any number of parameters. But, is divided into two types of parameters:

Positional parameters– list of parameters that can be accessed with index of parameter inside curly braces {index} ,See the example below.

Keyword parameters– list of parameters of type key=value, that can be accessed with key of parameter inside curly braces {key}

See the example :

we have another method of formatting string literals , as shown below:

You could also call functions as shown below:

Hope you have enjoyed the post,please share your comments

阅读原文...


sqlzealots

创新工场徐天施:中国供应链建设还需向先进学习

上一篇

美国空军正在研究真正会思考的人工智能

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Format Strings in Python

长按储存图像,分享给朋友