In Python, what does % mean? In python, how does one use semicolons ?, Python percent sign

Source: Internet
Author: User

In Python, what does % mean? In python, how does one use semicolons ?, Python percent sign

Two common

Type 1: Numeric operation 1% 3 refers to the modulo operation, with the remainder (remainder)

>>> 7% 2
1

#-*-Coding: UTF-8-*-'''python reads a file, outputs a file in an even row, and outputs a file in an odd row ''' def fenhang (infile, outfile, outfile1 ): infopen = open (infile, 'R', encoding = 'utf-8') outopen = open (outfile, 'w', encoding = 'utf-8 ') outopen1 = open (outfile1, 'w', encoding = 'utf-8') lines = infopen. readlines () I = 0 for line in lines: I + = 1 if I % 2 = 0: outopen. write (line) else: outopen1.write (line) infopen. close () outopen. close () fenhang ("source file path", "even number of lines file path", "odd number of lines file path ")

Specific can refer to this article: http://www.bkjia.com/article/136704.htm

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

The question is the % operator (string formatting, string formatting), which is described as follows:
% [(Name)] [flags] [width]. [precision] typecode
(Name) is named
Flags can include +,-, '', or 0. + Indicates the right alignment. -Indicates left alignment. ''Is a space, indicating that a space is filled on the left of a positive number to align with a negative number. 0 indicates filling with 0.
Width indicates the display width.
Precision indicates the decimal point precision.

The following is the type code
% S string (displayed using str)
% R string (display with repr)
% C single character
% B binary integer
% D decimal integer
% I decimal integer
% O octal integer
% X hexadecimal integer
% E index (base write as e)
% E index (base write as E)
% F floating point number
% F floating point number, same as % g index (e) or floating point number (based on the display length)
% G index (E) or floating point number (based on the display length)
% Character "%"

Example

>>> print("%6.3f" % 2.3) 2.300 

# The content after the first "%" is the display format description. 6 is the display width, 3 is the number of decimal places, and f is the floating point type.
# The second "%" is followed by the displayed content source, and the output result is right aligned. The length of 2.300 is 5, so there is a space in front.

>>> print("%+10x" % 10)    +a

# X indicates hexadecimal notation, with a display width of 10 and eight leading Spaces

>>>print("%-5x" % -10)-a  

# "%-5x" negative signs are left aligned, and the display width is 5. Therefore, there are 3 spaces behind-

>>> Pi = 3.1415 >>> print ("the pi value is % s" % pi)

The pi value is 3.1415.

>>> Print ("pi value is %. 8f" % pi) pi value is 3.14150000

The width and precision values above are two integers. We can use * to dynamically substitute these two quantities. For example:

>>> print("%10.*f" % (4, 1.2))  1.2000

Below is a supplement

Simply put, this is an operation to place other variables into a specific position of a string to generate a new string. For example:

n = "Aki""My name is %s" % n

This code first defines a variable named n with the content of Aki. Then there is a % s in the string below, which means "here will be replaced with a new string". The content used for replacement is placed after the % after the string, that is, the n. So this string will eventually become My name is Aki.

A letter is appended with % in the string, representing the type of the variable to be replaced. For example, % d indicates that the variable you will replace here is an integer, % s represents a string. For details, see here. Bytes. The following are some examples.

>>> "Www. % s.net "% (" jb51 ") # string 'www .jb51.net' >>>> "% dkm" % 1990 # integer '000000' >>>> "% s % d % f" % ("abc", 1990km, 3.21) # multiple values 'abc 123 123'

In python3

# -*- coding: UTF-8 -*-print("www.%s.net") %("jb51")

Output www.jb51.net

What does % s % in python mean?

It is a string formatting syntax (it is borrowed from C ).

See "format strings ":

Python supports formatting values into strings. Although this can include very complex expressions, the most basic usage is to insert the value into the string of the % s placeholder.

Edit: This is a very simple example:

This % s token allows me to insert (and potentially formatted) strings. Note that the % s token is replaced with the % symbol and is passed to any content of the string. Note that I also use a single tuples here (when you only have one string that uses the tuples is optional) to describe how to insert and format multiple strings in a statement.
To help you learn more, the following describes how to use multiple formats in a string:

If you use int instead of string, use % d instead of % s.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.