Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Ask about the usage of % in python
Ask about the usage of % in python

Two common usages of %:

1. Numerical operation 1 % 3 refers to modular operation, taking the remainder (remainder)

>>> 7 %2

1

2. String operation 'abc %s' % 'abc' '%s' is similar to a placeholder, the result of this line of code.

The following are type codes:

%s string (displayed using str())

%r string (displayed using repr())

%c single character

%b binary integer

%d decimal integer

%i decimal integer

%o octal integer

%x hexadecimal integer

%e exponent (base written as e)

%E exponent (base written as E )

%f floating point number

%F floating point number, the same as above %g exponent (e) or floating point number (according to the display length)

%G Exponent (E) or floating point number (according to display length)

%% character "%"

Extended information

" %S"% usage in PYTHON :

A string formatting syntax. The basic usage is to insert the value into the string of %s placeholder.

%s, means formatting an object as characters

"%± (positive and negative signs) 3 (numbers indicating the length of the string) s"% (characters that replace s String)

%s ?string type? means formatting an object as the character "%s1"%S2? s1 places a string (formatted string)? S2 places a desired Formatted value

string = "good"? #Type is string

print("string=%s" %string) #The output print result is string=good?

print("string=%3s" %string) #The output print result is string=good (the number 3 means: the length of the string is 3.

When the length of the string is greater than 3, the result is printed according to the length of the string)

print("string=%(+)6s" %string)? # The output print result is string=? good (When the length of the string is less than 6, spaces are filled on the left side of the string so that the length of the string is 6)

print("string=%-6s" %string)? # Output The print result is string=good? (When the length of the string is less than 6, spaces are filled on the right side of the string so that the length of the string is 6)