Small knowledge, big challenge! This article is participating in Essential knowledge for programmers Creative activities
This article has participated "Digging Star Program" , Win the creative gift bag and challenge the creative incentive fund.
Chapter 22 of the official Python column, students stop. Don't miss this article starting from 0!
The previous article analyzed the simple use of strings. In this article, let's look at the formatting of strings
What is string formatting and why?
Sometimes we shake the /B station tiktok to see the cover is very nice, but found it in live broadcast, but it is so!
The anchor must somehow convert the output into what the reader wants to see.
Have you roughly understood what is formatting and the necessity of formatting!
Just analogy, the formatting of our program is not so excessive! 1 output is still 1, but human beings understand it better.
Unlike the slightly formatted network anchor (the project is very large), it has changed from 1 to 0.
After all, programs can't deceive developers.
Necessity: the output is better formatted to facilitate developers and users to understand the output.
What formatting does the program have?
There are three or four online, and other libraries may be added in the future.
The school committee has developed many languages, which can be summarized into the following two categories:
- String placeholder replacement
- String keyword replacement
Let's look at the code
Placeholder formatting
First% placeholder formatting
The most common placeholder in python is'% '
str = "hello" "this is a string: %s" % str "this is a string: %s, %s" % (str, str)
From the above observation, we can find that '% s' in the output is replaced by' hello ';
The other is the replacement of multiple '% s', which is to replace with a tuple according to the corresponding subscript.
Formatting can basically be done using '% s'.
OK, after understanding this, let's continue to look at the following code:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 10:13 am on October 30, 2021 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Thunder Science Committee # @XueWeiTag: CodingDemo # @File : string_format.py # @Project : hello import sys first = "Continuous learning" second = "Continuous development" slogan = first + second print(slogan) banner = "*" * 16 print(banner) slice = slogan[2:4] print(slice) print(""""study" in slogan : %s""" % ("study" in slogan)) print(""""No learning" not in slogan : %s""" % ("No learning" not in slogan)) print(r"""Print\n Line breaks are output as normal characters!""") # As we can see from the above%, the code has talked about this many times, but there is no too much explanation print("%s" % slogan) # Most commonly used% s format string # print("%c"%'ccc')#TypeError: %c requires int or char print("%c" % 'c') # %c is usually used to force the string to be output to be 1 in length print("%c" % 'thunder') # %c is usually used to force the string to be output to be 1 in length number = 102.40101 print("%%i Signed integer %i" % number) print("%%i Signed integer:%i" % -number) print("%%d Signed integer %d" % number) print("%%d Signed integer %d" % -number) print("%%u Unsigned integer:%u" % number) print("%%u Unsigned integer:%u" % -number) #print("octal%o"% number) print("%%o octal number system %o" % 102) print("%%x16 Base system %x" % 102) #Print ("hex% X"% 102) print("%%e Natural constant e Base: %e" % number) #print("%E" % number) print("%%f Floating point number %f" % number) print("%%g Flexible and effective display:%g" % number) #On the premise of ensuring the display of 6 micro significant digits, the decimal method or scientific counting method shall be flexibly selected #print("%G" % number) print("%%g Flexible and effective display:%g" % (number*10001)) #On the premise of ensuring the display of 6 micro significant digits, the decimal method or scientific counting method shall be flexibly selected #The following two methods need to pay attention to the execution order #print("%g" % number*10001) #Pay attention to this way of writing #print("%g" % number**10) #Pay attention to this way of writing
Readers can directly copy the operation code, and the school committee has supplemented the operation effect diagram:
What is special here is the display of the unsigned integer '% u'. There is no difference between positive and negative numbers.
That's for sure. Unsigned and signed integers are two means of representing numbers.
But unsigned ones have one more bit to put numbers than signed ones, so the range is larger.
Let's take a look at or skip the above digression. Let's continue to look at other formatting methods.
Second placeholder formatting
The slight difference from the above is that we will see an obvious subscript.
{0}, {1}, ... {n}
If you want to leave several replacement bits, put a few {} in it, but you must give n parameters. For more information, please refer to https://docs.python.org/3/library/string.html
This is very simple and direct. Let's look at the code:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 10:13 am on October 30, 2021 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Thunder Science Committee # @XueWeiTag: CodingDemo # @File : string_format1.py # @Project : hello import sys slogan = "Continuous learning and continuous development" author = "Lei Xuewei" text = "{0}, {1}".format(slogan, author) print("text= %s" % text)
Keyword formatting
It is very easy to understand that it is similar to {} a pair of large parentheses with the content to be replaced in the middle.
This is very simple and direct. Let's look at the code:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 10:13 am on October 30, 2021 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Thunder Science Committee # @XueWeiTag: CodingDemo # @File : string_format2.py # @Project : hello import sys slogan = "Continuous learning and continuous development" author = "Lei Xuewei" text = f"{slogan} , {author}" print("text= %s" % text)
The operation effect is as follows:
From here, keyword formatting is more friendly!
Next
There must be many formatted libraries in Python ecology, which are not covered one by one.
Readers can refer to these two ideas to customize and develop their own alternative formatting scheme and publish it as a library. Here, the academic committee should recommend this article on developing python Library:
Nanny level tutorial takes you to develop high-quality Python Library Part 2 [seed project]
The next article will introduce other functions for writing string operations.
By the way, if you like Python, please pay attention to the school committee Python foundation column or Introduction to Python to master the big column
Continuous learning and continuous development, I'm Lei Xuewei!
Programming is very interesting. The key is to understand the technology thoroughly.
Welcome to wechat, like and support collection!