Format the string to make your string look good

Format the string to make your string look good


Formatting strings is also an inconspicuous but very useful function in daily life. Literally speaking, formatting a string means outputting the string in a certain format to "beautify" the string.

Small scale chopper

I encountered such a case in my work before, and the number 1-100 needs to be coded as 001-100.


In fact, there is a rule here, that is, these numbers all occupy three places, just use 0 to fill in the front.

(1) But before I learned string formatting, I first thought this way. To determine the size of the number, add two 0s before the number is less than 10, and add one 0 if the number is less than 100, and just use the string to collage.

for i in range(1,101):
    if i <10:
    elif i <100:

But this way of writing is very bad. If I want to output 100,000, then I have to write many judgments, the code will be very long, and there is no scalability.

(2) Based on the determination of the first method, improvements have been made. By calculating the length of the number, because the occupying position is 3 digits, subtracting the length of the number from 3 is the number that needs to be filled with 0.

for i in range(1,101):
    print("0"*(3-len(str(i))) + str(i))

Writing in this way can solve the shortcomings of the first method, and the code is very concise.

(3) But after learning string formatting, I found that python has already given us a method, and it can be output directly in a certain format.

for i in range(1,101):

I won't explain the usage first. I want to go back and look at this code through the detailed explanation below. Readers should know why they wrote it this way, and they will also write string codes with various formats.

Format string

First of all, what I want to say is that there are many ways to write the format string itself, and today I will only talk about the three most commonly used methods.

  • %character
  • format function
  • f-string

The% character is easy to write, and some big guys are accustomed to using this. He is an old antique in python2, so many python veterans have become accustomed to this method. Let's look at the code first for an example.

name ='luopan'
age = 27
print('My name is %sI am %d years old' %(name,age))

# My name is luopan.I am 27 years old

The core paradigm is:

'%x %x' %(v1,v2)

The quotation marks are the string to be formatted, %x is the specified format, %s represents the format string, %d formats integers, and %f formats floating-point numbers.

format function

Starting from Python2.6, a new function format() for formatting strings has been added, which enhances the function of string formatting. This is also my usual method of formatting strings.

name ='luopan'
age = 27
print('My name is {}.I am {} years old'.format(name,age))

# My name is luopan.I am 27 years old

The basic syntax is to replace the previous% with {}.

The core paradigm is:

'{} {}' .format(v1,v2)

Since the format function is my commonly used method of formatting strings, I will introduce the usage of the format function in detail here.

First of all, the format function accepts an unlimited number of parameters, and the order can also be specified.

'{} {}'.format('hello','world') 
# hello world

'{1} {0}'.format('hello','world') #specify order
# world hello

Of course, it is also possible to pass in dictionaries and lists.

info = {
    'age': 27

# name:luopan,age:27

info = ['luopan',27]

# name:luopan,age:27

Speaking of this, we have only learned the method of the format function, and have not yet learned how to format it in the specified format. Let's explain it next.

(1) Specify the number of decimal places

a = 1.34236

# 1.342

The format that needs to be defined is followed by the: number.

(2) Specify the width

a = 3

# xxx3

Here complement x,> represents the left side complement x, 4 represents the width is 4. I think you should understand what the previous :03d means when you see it here (the width is 3, and the front is 0).

Of course there are many other formats, please refer to this article ( ) for details .


The f-string method is more concise, and it is now the most recommended method for everyone. Take the above code as an example.

name ='luopan'
age = 27
print(f'My name is {name}.I am {age} years old')

# My name is luopan.I am 27 years old

Just add f in front of the string, and the variable can be passed directly in the curly braces after it.

That's it for today's sharing, see you next time!

Reference: format strings to make your strings look good-Cloud + Community-Tencent Cloud