Methods for Python strings, finding and replacing

Source: Internet
Author: User

PythonDigital

integer int such as:1 4 8

floating-point float :1.0 3.6 78.9

Long integer , such as:1L 37565846588L(after an integer plus L is the long integer type, theoretically long integer length of 2147483647 )

Note: in When Python is operating on MySQL , the number of exported shapes may be long integers.

Command (IDLE(Python GUI))

Print prints out the specified content

format:print ( content )

type to return the object or instance

Len Returns the length of the specified sequence

Define numbers

Print out the value content using the Print command:

use the type command to return the data type of these three numbers:

Conversion of numeric types: Convert by type function

Conversion of numeric types: Conversions by Operation: What type of divisor is this number?

Arithmetic of numbers

Numbers are calculated in the following ways:

Plus +

minus -

Multiply *

except /

divisible //

% of remainder

Request Cubic * *

The operation between data is as follows:

string

The so-called string is a series of ordered, non-modifiable, elements surrounded by quotation marks.

There are several types of string definitions:

single quotation marks ' string '

Double quotes " string "

Double single quotes "' string '

Double quotes "" " string " ""

type function str

The string is defined as follows:

The differences between the four definitions:

1, single and double quotation marks are used to distinguish between the two in the code of the simultaneous occurrence of ambiguity

The first is an error, because there are three single quotes, which do not think the middle single quotation marks are symbols in the string.

2, three single quotes and three double quotes are also used to distinguish between the two in the code of the simultaneous occurrence of ambiguity

3, three quotation marks (three single quotes and three double quotation marks) to form a multiline string with line wrapping

Note: in Python can also use three double quotation marks ("" ") to annotate the code

index of the string

All ordered sequences are indexed, and the difference is whether the sequence can be modified, and the index is understood as the subscript for each character. Each individual in a string can be considered an element, and each element has its corresponding index.

Note: The index is starting from 0 .

Use of indexes:

1. Take a single element

string        [ index value ] take the element corresponding to the index

2. intercept a certain character

string    [Start:end] gets the element of the specified range,start contains the element itself, andend does not contain its own element

3. Step intercept

string [Start:end:step] Step is the step size, which is separated by a step minus 1

4. Default Extraction

start:0 End: End Step:1

5. Take counter

string   [ Negative ] take from right to left

method of String

1,center Let the string in the specified length center, you can specify the contents of the fill

2. Ljust Let the string be left aligned at the specified length, you can specify the contents of the fill

3. Rjust Let the string be right aligned at the specified length, you can specify the contents of the fill

4,Zfill the string to the specified length, the insufficient place starts from the left with 0 fills

5. Format passes the specified parameters to the curly braces in the preceding string ,in order

6,strip default to remove the space on both sides, the content can be removed to specify

7,lstrip default to remove the left space, the content can be removed to specify

8,rstrip default to remove the right space, the content can be removed to specify

Searching for strings

1,count returns the number of occurrences of the specified character in the string

2,find returns the first occurrence of the specified character from the left index, can not find the return -1

3,rfind returns the index from the first occurrence of the specified character from the right, and cannot find the return -1

4,index Returns the first occurrence of the specified character from the left index, can not find an error

5. Rindex Returns the index of the first occurrence of the specified character from the right, unable to find an error

Substitution of strings

1,replace replaces a character in a string with a specified character, and can also specify the number of substitutions for the specified character

2,translate in accordance with the specified good correspondence to replace the content


Methods for Python strings, finding and replacing

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.