Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the detailed explanation of the string formatting format function in Python3

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you a detailed explanation of the string formatting format function in Python3. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Format qualifier

Format uses rich "format qualifiers" (syntax is {} with: sign) to complete more detailed formulation of the content that needs to be formatted.

Binary conversion

We can set different characters in the qualifier to format the binary conversion of numbers, and the table corresponding to the base:

Character meaning b binary cUnicode character d decimal integer o octal x hexadecimal, a to f lowercase X hexadecimal, A to F uppercase

N = 99print ('{: B} '.format (N)) print (' {: C} '.format (N)) print (' {: d} '.format (N)) print (' {: o} '.format (N)) print (' {: X} '.format (N)) print (' {: X} '.format (N))

Example result:

1100011c991436363

Fill and align

The character with padding after the: sign can only be one character. If not specified, it is filled with spaces by default, and the padding is often used with alignment. ^, are centered, left-aligned, right-aligned, and followed by width.

N = 99print ('{: > 8} '.format (N)) print (' {: > 8} '.format (N)) print (' {:-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report