Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the usage of% in python?
What is the usage of% in python?
% in python print format has the function of converting characters:

(1)%c integer is converted into corresponding ASCII characters;

(2)%d integer is converted into decimal;

(3) The number with the precision of% f times is converted into floating point number;

(4)%o integer to octal;

(5)%s integer is converted into a string;

(6)%x integer is converted into lowercase hexadecimal;

(7)%X integer is converted to uppercase hexadecimal.

For example:

test

"Print" it is a% s "%(a)

The result of printing is that it is a test.

Usage of "%S" in PYTHON:

Syntax of string format. The basic usage is to insert the value into the string of the %s placeholder.

%s, which means formatting objects as characters.

"%(symbol) 3 (number indicates the length of string) s"% (string instead of s)

%s? String type? Indicates that the object is formatted as the character "%s1"%s2? S 1 put a string (formatted string)? S2 places a value to be formatted.

String = "good"? # Type is a string.

Print("string=%s" %string) # Output print result is string=good?

Print("string=%3s" %string) # The print result is string=good (the number 3 indicates that the length of the string is 3. When the string length is greater than 3, print the result according to the string length)

print("string=%(+)6s" %string)? # The printed result of output is string=? Ok (when the string length is less than 6, fill the space on the left side of the string to make the string length 6)

print("string=%-6s" %string)? # The printed result is string=good? (When the string length is less than 6, fill the space on the right side of the string to make the string length 6)