search
HomeBackend DevelopmentPython TutorialDetailed introduction to format() format output in Python (with code)

This article brings you a detailed introduction to format() format output in Python (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Formatted output: format()

format(): Replace the traditional % with {} to achieve formatted output

1. Use positional parameters: that is, In the string, replace the variable value that needs to be output with {}, and then use format() to modify it to become the desired string. The positional parameter is to change the traditional % to {}, and automatically replace it according to the positional order.

'My name is {},age:{}'.format('Anxc',18)
'My name is Anxc,age:18'

2. Use positional parameters: On the original basis, the replacement value is changed according to the position by changing the position (I feel it is useless, not as useful as the first one)

'My name is {1},age:{0}'.format(18,'Anxc')
'My name is Anxc,age:18'

3. Character padding (left-aligned, right-aligned, center-aligned)

<span style="color: #800000">&#39;右对齐{:#>10}&#39;.format(10)<br/>&#39;右对齐########10&#39;</span>

4. Use keyword parameters: use key=value to achieve one-to-one assignment replacement

&#39;My name is{name},age:{age}&#39;.format(name=&#39;Anxc&#39;,age=18)
&#39;My name isAnxc,age:18&#39;

5. Precision output of numbers: It feels like the float type output of C language. (Format: {:. #binary

b

##octalHexx
>>> &#39;{:.4f}&#39;.format(1/3)
&#39;0.3333&#39;
>>> &#39;{:4f}&#39;.format(100)
&#39;100.000000&#39;#目前没有发现整数是啥用处
>>> '{:,}'.format(19012390123)
'19,012,390,123'
9. Formatting through the properties of objects (I haven’t learned python classes yet, so there are no examples)
o

decimal

7. Thousandth division of numbers

&#39;18的二进制:{:b}&#39;.format(18)
&#39;18的二进制:10010&#39;
>>> &#39;18的八进制:{:o}&#39;.format(18)
&#39;18的八进制:22&#39;
>>> &#39;18的十六进制:{:x}&#39;.format(18)
&#39;18的十六进制:12&#39;

8. Formatting through subscripts

The above is the detailed content of Detailed introduction to format() format output in Python (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)