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 are the three output formats in python

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

Share

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

This article mainly introduces "what are the three output formats in python". In daily operation, I believe many people have doubts about what the three output formats are in python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the three output formats in python?" Next, please follow the editor to study!

Three output formats of python

Environment: pycharm + python3.8

1.% (not recommended)

Format: format string% (output item 1, output item 2, … Output item n).

C characters s string d signed integers (10) o signed integers (8) x or X signed integers (16) f or F floating point numbers

Use:

Case one

Num1= 20num2 = 30print ('num1=%d, num=%d'% (num1, num2))

Output:

Num1=20, num=30

Case two

Num = 3.141526print ('% 0.2f'% num) # keep two decimal places print ('.1f'% num) # occupy 10 spaces, right-aligned print ('%-10.2f'% num) # occupy 10 spaces, align left

Output:

2. Format

Format: format (output item [, format string]), where the format string is selectable.

Case one

Num1= 20num2 = 30print ('num1= {}, num2= {}' .format (num1, num2))

Output:

Num1=20, num=30

Case two

Str1 = 'small bath' str2 = 'small cold' print ('{0} {1} {1} {0} '.format (str1, str2)) # mapping variable values from 0

Output:

A little bath, a cold bath.

Case three

Print ('{meng} {mu} '.format (mu=' Xiaomu', meng=' Xiaomeng'))

Output:

Xiaomeng Xiaomu

Case four

Right alignment ^ middle alignment = (for numbers only) to complete # 0displacement 3.14 after the decimal point | *: fill in extra blanks (easy to observe) | print ('--{0displacement 7} '.format)

Output:

Case five

Print ('decimal: {0 d}, hexadecimal: {0 x}, octal: {0 o}, binary: {0 b}' .format (31)) # with'# 'with the prefix print (' hexadecimal: {0orex}, octal: {0freuo}, binary: {0freb} '.format (31))

Output:

Decimal: 31, hexadecimal: 1f, octal: 37, binary: 11111

Hexadecimal: 0x1f, octal: 0o37, binary: 0b11111

Case 6

# convert to percentage and retain two decimal places print ('percentage: {: .2%}' .format (0.555555)) # retain two decimal places print ('retain two decimal places: {: .2}' .format (0.555555))

Output:

Percentage: 55.56%

Keep two decimal places: 0.56

Case 7

Print ('{:} *-* {:} '.format (1,-1)) # shows the symbol print (' {: +} *-* {: +} '.format (1,-1)) # if it is a positive number, leave the space print (' {:} *-* {:} '.format (1,-1)) # -, and' {:} {:} 'consistent print (' {: -} *-* {: -} '.format (1,-1))

Output

3. F-string

Usage: f'{}'

Case one

Name = "small bath" age = 20print (f'my name is {name}, I am {age} this year.')

Output:

My name is Xiao Mu. I'm 20 years old this year.

Case two

Name = "small bath" age = 20print (f'my name is {name},'f'is {age} this year.') # Multiline usage

Output:

My name is Xiao Mu. I'm 20 years old this year.

Case three

Num = 3.141526print (F' retains two decimal places: {num:.2f}')

Output:

Keep two decimal places: 3.14

For more usage, please refer to the second point above: format

Contrast

F-string: formatted string literals, formatting string constants.

The function is the same as%-formating, str.format (), easy to operate and fast.

At this point, the study on "what are the three output formats in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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