How to use the format function in python

Starting from Python2.6, a new string formatting function str.format() has been added, which enhances the function of string formatting.

The basic syntax is to use {} and: to replace the previous %.

The format function can accept an unlimited number of parameters, and the positions can be out of order.

E.g

"{} {}". format("hello","world")  #Do not set the specified location, follow the default order
' hello world'"{0} {1}".format("hello","world") #Set the specified location
' hello world'"{1} {0} {1}".format("hello","world") #Set the specified location
' world hello world'

Parameters can also be set

#! /usr/bin/python
# - *- coding: UTF-8-*-print("Site name:{name},address{url}".format(name="python learning network", url="www.py.cn"))
# Set parameters via dictionary
site ={"name":"python learning network","url":"www.py.cn"}print("Site name:{name},address{url}".format(**site))
# Set parameters by list index
my_list =['ZaLou.Cn','www.zalou.cn']print("Site name:{0[0]},address{0[1]}".format(my_list)) # "0"It's required

Output result

Website name: ZaLou.Cn, address www.zalou.cn
Website name: ZaLou.Cn, address www.zalou.cn
Website name: ZaLou.Cn, address www.zalou.cn

So far, this article on how to use the format function in python is introduced. For more related python format function usage content, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. Cn!

Recommended Posts

How to use the format function in python
How to use the round function in python
How to use SQLite in Python
How to use and and or in Python
How to use code running assistant in python
How to install the downloaded module in python
How to use python tuples
How to understand the introduction of packages in Python
How to use Python's filter function
How to use python's help function
How to use hanlp in ubuntu
How to use python thread pool
How Python implements the mail function
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How Python implements the timer function
How to view the python module
How to understand variables in Python
How to clear variables in python
How to find the area of a circle in python
How to introduce third-party modules in Python
How to represent null values in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
Python novice learns to use the library
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
What is the function of adb in python
How to configure TensorFlow use environment in Ubuntu
Join function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
An article to understand the yield in Python
How to use dpkg command in Ubuntu system
How to open the ubuntu system in win10
How to enter python through the command line
How to set code auto prompt in python
How to switch the hosts file using python
Teach you how to write games in python
How to delete files and directories in python
Introduction to the use of Hanlp in ubuntu
How to write a confession program in python
Python how to move the code collectively right
The best way to judge the type in Python
Use python to realize the aircraft war game