SoFunction
Updated on 2024-11-13

Python string output in three formats

String formatted output is a very important basic syntax of python, today the three kinds of formatted output to do a brief summary, I hope to help you.

Formatted Output: Content is output according to certain formatting requirements.

1. Use the placeholder % output

Prior to python version 2.6, formatting strings with % followed the C output format.

Instructions for use:

print("Formatting string" % variables)

# Variables more than 2 use tuple format:

print("Formatting string" % (variable1,variable2))

Use the % placeholder to indicate the position of a variable in a string.

The value passed in should correspond to the % placeholder variable.

where %s denotes a string, %d denotes an integer, %f denotes a decimal (the default retains six decimal places, and %.2f retains two decimal places), and %% is required to denote a percent sign when a formatting flag exists.

name='xiaoming'
age=12
print("My name is %s,My age is %d" %(name,age))
#exports:My name is xiaoming,My age is 12

formatting

format is a new method of formatting strings in python 2.6, which has the following advantages over %format:

  • A single parameter can be output multiple times, and the order of the parameters can be different.
  • Filling is very flexible and alignment is very powerful
  • The official recommended way

Instructions for use:

print("...{indexing}, ... , {indexing}, ...".format((be) worth1, (be) worth2))
#Index {} is empty, default values are taken in order.
print("...{key1}, ... , {key2}, ...".format(key1=value,key2=value))
name='xiaoming'
age=12
print('My name is {}, My age is {}'.format(name,age))
print('My name is {0}, My age is {1}'.format(name,age))
print('My name is {name}, My age is {age}'.format(name='xiaoming',age=12))
#exports:My name is xiaoming,My age is 12

Format Advancement

1. Fill Alignment

# First get the value, then set the padding format after the colon: {index:[padding character][alignment][width]}
# *<20: left-justified, total of 20 characters, not enough to fill in the * sign
print('{0:*<20}'.format('hellopython'))
# *>20: right-aligned, total of 20 characters, not enough to fill in the * sign
print('{0:*>20}'.format('hellopython'))
# *^20: centered, total of 20 characters, with *'s for anything that's not enough.
print('{0:*^20}'.format('hellopython'))
exports:
hellopython*********
*********hellopython
****hellopython*****

2. Bit and Conversion

# Retain 2 valid digits
print("{:.2f}".format(3.1415926))
# Convert to binary
print('{0:b}'.format(16))
# Convert to octal
print('{0:o}'.format(10))
# Convert to hexadecimal
print('{0:x}'.format(15)) 
exports
3.14
10000
12
f

f-string formatting

The introduction of f-strings in Python 3.6 is not only easier than using them, but also more efficient.

Instructions for use

An f-string is a string preceded by "f", and {} is used directly with variables, expressions, and so on.

name='xiaoming'
age=12
Direct use of variables in #{}
print(f'My name is {name},My age is {age}')
Running expressions in #{}
print(f'{1+2+3}')
# Calling Python's built-in functions
print(f'{()}')
#Anonymous functions with lambda: can do complex numerical calculations
fun = lambda x : x+1
print(f'{fun(age)}')
# Output
My name is xiaoming,My age is 12
6
XIAOMING
13

summarize

to this article on the Python string three formatting output article is introduced to this, more related Python string formatting output content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!