python% formatters Output:
Syntax, basic usage is to string formatting values into strings% s placeholders.
- % S, formatting represents a character object
- "% ± (sign represented) 3 (the numbers indicate the length of the string) s"% (substituted string s)
- % S string type indicates format of an object character "% s1"% S2 s1 is placed a string (format string) S2 is disposed a desired value to be formatted
Example 1:
# Formatted output name = INPUT ( " Please INPUT your name: " ) Print ( " ! The Hello,% S Good Morning " % name) # Output: Please INPUT your name: Xiaolizi # Enter the name Xiaolizi the Hello, Xiaolizi Good Morning! # print command
Example 2:
= String " Good " # Type string of Print ( " String% S = " % String) # print result is output string = good Print ( " String% 3S = " % String) # print result is output string = good (meaning the number 3 is: 3. when string length is greater than the length of the string 3, a print result according to the length of the string) Print ( " string% = (+) 6S " % string) # output print result string = good (character string when the length is less than 6, to fill the spaces on the left side of the string, such that the length of the string 6) Print ( " string% = - 6S " % string) # output print result string = good (less than when the length of the string 6, to fill the space on the right side of the string, such that the length of the string 6) #The number after the decimal point string length taken Print ( " String. 3% = (. 6) S. " % String) # print result is output string = goo (good) (% 3s means: character string taken the first three characters, the character string is taken when the length is greater than the string, the whole string is output results) Print ( " string% a.bs = " % string) # first string taken according to figures after the decimal point B, when taken string length is less than a, the need to fill the spaces on the left side of the string, such that the length of the string becomes a Print ( " string =% *. S * " % (. 6,. 3, string)) # % *. * s represent precision, two values are represented by% * foregoing string values are separated by commas to specify two