Python basics 6: formatting characters, colors, and python Basics
Character formatting:
1.% placeholder, % s, % d, %
2. Use format. The format is easy to use. It can be centered, %, binary, and character-filling;
1. Use Case of %
Tp1 = "I am % s" % "aaa "#
Tp2 = "I am % s age % d" % ("alex", 18) # sequential Association
Tp3 = "I am % (name) s age % (age) d" % {"name": "alex", "age": 18} # specify the name and name
Tp4 = "percent %. 2f" % 99.567 # number of decimal places reserved
Tp5 = "I am % (pp). 2f" % {"pp": 12.45667,} # specify the name and retain two decimal places
Tp6 = "I am % (pp). 2f %" % {"pp": 13.34566, }# use double % to reference %
Print ("tp1:", tp1)
Print ("tp2:", tp2)
Print ("tp3:", tp3)
Print ("tp4:", tp4)
Print ("tp5:", tp5)
Print ("tp6:", tp6)
Execution result:
2. Use format
Tp1 = "I am {}, age {}, you are {}". format ("hhh", 123, "yyy") # sequential Filling
Tp2 = "I am {}, age {}, you are {}". format (* ["hhh", 123, "yyy"]) # dynamic parameter Filling
Tp3 = "I am {0}, age {1}, you are {0} too". format ("hhh", 123) # placeholder index filling, sequential Filling
Tp4 = "I am {0}, age {1}, you are {0} too ". format (* ["hhh", 123]) # placeholder index filling, dynamic parameter Filling
Tp5 = "I am {name}, age {age}, you are {name} too ". format (name = "hhh", age = 123) # specify the name to be filled and the name sequence is variable
Tp6 = "I am {name}, age {age}, you are {name} too ". format (** {"name": "hhh", "age": 123}) # name, dynamic parameter, Dictionary required **
Tp7 = "I am {0 [0]}, age {0 [1]}, you are {0 [2]}". format ([1, 2, 3], [11, 22, 33]) # pass through the list
Tp8 = "I am {: s}, age {: d}, money {: f }". format ("hh", 18, 88.11) # format character, S character, d integer, f floating point type
Tp9 = "I am {name: s}, age {age: d }". format (name = "hh", age = 18) # specify the name, S character, d integer, f floating point type
Tp10 = "I am {name: s}, age {age: d }". format (** {"name": "hhh", "age": 123}) # dynamic parameter + specified name, S character, d integer, f floating point type
Tp11 = "numbers: {: B}, {: o}, {: d}, {: x}, {: X}, {: % }". format (15,15, 15,15, 15, 3.666) # format characters, binary B, and integer d
Tp12 = "numbers: {0: B}, {0: o}, {0: d}, {0: x}, {0: X}, {0: % }". format (15) # format + index, B is in byte format, o is in octal format, and x is in hexadecimal format.
Tp13 = "numbers: {num: B}, {num: o}, {num: d}, {num: x}, {num: X}, {num: % }". format (num = 15) # format + specify name
Execution result:
Tp1: I am hhh, age123, you areyyy
Tp2: I am hhh, age123, you areyyy
Tp3: I am hhh, age123, you arehhh too
Tp4: I am hhh, age123, you arehhh too
Tp5: I am hhh, age123, you arehhh too
Tp6: I am hhh, age123, you arehhh too
Tp7: I am 1, age2, you are3
Tp8: I am hh, age18, money88.110000
Tp9: I am hh, age18
Tp10: I am hhh, age123
Tp11: numbers: 1111,17, 15, f, F, 366.600000%
Tp12: numbers: 1111,17, 15, f, F, 1500.000000%
Tp13: numbers: 1111,17, 15, f, F, 1500.000000%
Color format:
Format: echo "\ 033 [text background color; font color m input content \ 033 [0 m"
Case: echo "\ 033 [41; 36 m write something here \ 033 [0 m", where 41 represents the background color and 36 represents the color of the word
The ascii code is the beginning and end of the color call. \ 033 [; m ...... \ 033 [0 m
Case:
A1 = input ("\ 033 [41; 36 m write something here \ 033 [0 m") # Both foreground and background colors are set
A1 = input ("\ 033 [41; 1 m write something here \ 033 [0 m") # only set the background color and bold
A1 = input ("\ 033 [36; 1 m write something here \ 033 [0 m") # It can be individually identified to set only the font color and bold
A1 = input ("\ 033 [36; m write something here \ 033 [0 m") # You can identify and set only the font color, not bold
Color Range: 40----49
40: Black
41: dark red
42: Green
43: Yellow
44: Blue
45: Purple
46: dark green
47: white
Color: 30-----------39
30: Black
31: red
32: Green
33: Yellow
34: Blue
35: Purple
36: dark green
37: white
ANSI Control Code Description
\ 33 [0 m close all properties
\ 33 [1 m set high brightness
\ 33 [4 m underline
\ 33 [5 m flashing
\ 33 [7 m reverse display
\ 33 [8 m blanking
\ 33 [30 m -- \ 33 [37 m sets the foreground color
\ 33 [40 m -- \ 33 [47 m set the background color
\ 33 [move the nA cursor up n rows
\ 33 [move the nB cursor down n rows
\ 33 [move the nC cursor right n rows
\ 33 [nD cursor shifts n rows left
\ 33 [y; xH: Set the cursor position
\ 33 [2J clear screen
\ 33 [K clear content from cursor to end of line
\ 33 [s Save the cursor position
\ 33 [u restore cursor position
\ 33 [? 25l hide the cursor
\ 33 [? 25 h show cursor
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.