Python's powerful string formatting function-format
Since python2.6, a new function str.format() for formatting strings has been added, which is very powerful. So, what advantages does it have compared with the previous % formatted string? Let's unveil its shyness.
Syntax
It uses {} and: instead of %
Positional method formatting
>>> '{}.{}'.format('pythontab', 'com') 'pythontab.com' >>> '{}.{}.{}'.format('www', 'pythontab', 'com') 'www.pythontab.com' >>> '{1}.{2}'.format('www', 'pythontab', 'com') 'pythontab.com' >>> '{1}.{2} | {0}.{1}.{2}'.format('www', 'pythontab', 'com') 'pythontab.com | www.pythontab.com'
The format function of the string can accept unlimited parameters, the parameter positions can be out of order, and the parameters can be used or not. Multiple times, very flexible
Note: It cannot be empty {} under python2.6, but it can be used in python2.7 or above.
By keyword parameters
>>> '{domain}, {year}'.format(domain='www.pythontab.com', year=2016) 'www.pythontab.com, 2016' >>> '{domain} ### {year}'.format(domain='www.pythontab.com', year=2016) 'www.pythontab.com ### 2016' >>> '{domain} ### {year}'.format(year=2016,domain='www.pythontab.com') 'www.pythontab.com ### 2016'
By object properties
>>> class website: def __init__(self,name,type): self.name,self.type = name,type def __str__(self): return 'Website name: {self.name}, Website type: {self.type} '.format(self=self) >>> print str(website('pythontab.com', 'python')) Website name: pythontab.com, Website type: python >>> print website('pythontab.com', 'python') Website name: pythontab.com, Website type: python
By subscripts
>>> '{0[1]}.{0[0]}.{1}'.format(['pyhtontab', 'www'], 'com') 'www.pyhtontab.com'
With these convenient "mapping" methods, we have a lazy tool. Basic python knowledge tells us that lists and tuples can be "broken" into ordinary parameters for functions, while dict can be broken into keyword parameters for functions (through and *). So you can easily pass a list/tuple/dict to the format function, which is very flexible.
Format qualifiers
It has a wealth of "format qualifiers" (the syntax is {} with a symbol in it), such as:
Padding and alignment
Padding is often used together with alignment
^ , <、> are centered, left-aligned and right-aligned respectively, followed by width
: The padding character after the sign can only be one character. If not specified, it will be filled with spaces by default
Code example:
>>> '{:>10}'.format(2016) ' 2016' >>> '{:#>10}'.format(2016) '######2016' >>> '{:0>10}'.format(2016) '0000002016'
Number precision Precision is often used with type f
precision is often used together with type f
>>> '{:.2f}'.format(2016.0721) '2016.07'
where .2 represents the precision of length 2, and f represents float type.
Other types
are mainly based on base. b, d, o, and x are binary, decimal, octal, and hexadecimal respectively.
>>> '{:b}'.format(2016) '11111100000' >>> '{:d}'.format(2016) '2016' >>> '{:o}'.format(2016) '3740' >>> '{:x}'.format(2016) '7e0' >>>
Used, numbers can also be used as thousands separators for amounts.
>>> '{:,}'.format(20160721) '20,160,721'
format is so powerful and has many functions. You can try it out.

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

XML can be converted to images by using an XSLT converter or image library. XSLT Converter: Use an XSLT processor and stylesheet to convert XML to images. Image Library: Use libraries such as PIL or ImageMagick to create images from XML data, such as drawing shapes and text.

To generate images through XML, you need to use graph libraries (such as Pillow and JFreeChart) as bridges to generate images based on metadata (size, color) in XML. The key to controlling the size of the image is to adjust the values of the <width> and <height> tags in XML. However, in practical applications, the complexity of XML structure, the fineness of graph drawing, the speed of image generation and memory consumption, and the selection of image formats all have an impact on the generated image size. Therefore, it is necessary to have a deep understanding of XML structure, proficient in the graphics library, and consider factors such as optimization algorithms and image format selection.

There is no APP that can convert all XML files into PDFs because the XML structure is flexible and diverse. The core of XML to PDF is to convert the data structure into a page layout, which requires parsing XML and generating PDF. Common methods include parsing XML using Python libraries such as ElementTree and generating PDFs using ReportLab library. For complex XML, it may be necessary to use XSLT transformation structures. When optimizing performance, consider using multithreaded or multiprocesses and select the appropriate library.

XML formatting tools can type code according to rules to improve readability and understanding. When selecting a tool, pay attention to customization capabilities, handling of special circumstances, performance and ease of use. Commonly used tool types include online tools, IDE plug-ins, and command-line tools.
