Python string-built-in method usage analysis

Source: Internet
Author: User

1. Letter case-sensitive (Chinese invalid)

1.1 S.upper ()-String

Returns a copy of all uppercase letters

1.2 S.lower ()-String

Returns a copy of a letter that is all lowercase

1.3 S.swapcase ()-String

Returns a letter case-converted Copy

1.4 S.title ()-String

Capitalize the first letter of the word, which is the so-called title

In the box is the Chinese code, you can find the s or uppercase, the description will ignore other types of characters, find English words to capitalize their initials

1.6 S.capitalize ()-String

Capitalize the first letter of the string, noting that not all words, attention and the difference above

And it's not valid when the first character of the string is not a letter.

1.5 S.isupper (), BOOL

Determines whether the letters in the string are all uppercase, must have letters in the string, and returns a Boolean value

This is the case when mixing with other characters

Ignores other characters and only determines the letters.

1.6 S.islower (), BOOL

Determines whether the letters within the string are all lowercase, must have letters in the string, and returns a Boolean value

and capitalization judgment is the opposite, here does not continue to the example.

1.7 S.istitle (), BOOL

To determine whether a string conforms to the title format, returns a Boolean value, and describes in detail what is heading in the Python string, not much here.

2. String composition character judgment

Characters in a string, in addition to the English alphabet, can have other characters, and there are ways to judge the constituent characters of a string.

2.1 S.isalpha (), BOOL

Determines whether a string is entirely composed of letters , returns a Boolean value

    

Only the whole letter, there are numbers and Chinese and so on.

2.2 S.isdigit (), BOOL

Determines whether a string is entirely composed of numbers , returns a Boolean value

Similar to the above, not much explanation

2.3 S.isalnum (), BOOL

Determines whether a string is entirely composed of letters and numbers , and returns a Boolean value

is a merger of the above two judgments

2.4 S.isspace (), BOOL

Determines whether a string is composed of null characters and returns a Boolean value

  

Null character does not mean that the string is empty, such as a space, \ n and other special characters nonalphanumeric is considered null characters

3. Whether a string is judged at the beginning or end of a particular character

3.1 S.startswith (prefix[, start[, end]), BOOL

Determines whether to start with a specific character, returns a Boolean value

By default, starting with the first character, which is the index value of 0, you can use the index to specify a range of characters to judge

3.2 S.endswith (suffix[, start[, end]), BOOL

Determines whether to end with a specific character, returns a Boolean value

Similar to the above, not much explanation

There is so much to judge in the string, and here's a summary of what you can tell:

1. Whether the letters in the string are all lowercase or uppercase

2. Whether a string conforms to the title format

3. Whether the composition of the string is full-letter, full-digit, or full of letters and numbers, or all empty characters

4. Whether the string starts or ends with the given character

4. Searching for a given character within a string

4.1 S.find (Sub [, Start [, end]]) int

Look for the given character within the original string, find the first, return its index value, not found return-1

Note is looking from left to right , find the first , like here O, in the original string has more than one, but only returns the index value of the first, in addition, when the given character is more than one, the first character of the given string as the datum point, as here the CO returns the index value of C. Of course you can specify a range, but specifying a range does not mean that the index value will be recalculated, and the resulting index is still relative to the entire string.

4.2 S.rfind (Sub [, Start [, end]]) int

The same as above, but is looking from right to left .

4.3 s.index (sub [, Start [, end]]) int

and the Find () method is basically the same, except that find () is not found when the return is-1, and index () can not find the error.

4.4 S.rindex (Sub [, Start [, end]]) int

In the same way, this is from right to left.

5. Count of occurrences of a string

And look for similar, but the method described above is only to find the first one, if I want to know how many times a particular string has occurred in the original character, you can use the count.

5.1 s.count (sub[, start[, end]) int

This method finds all specified substrings in the range, returns the number of occurrences, and returns 0 if none is found.

6. Substitution of characters

Note that although the original string is immutable, as I said in the previous article, all methods of the string did not change the original string itself, and all returned a new object.

The so-called substitution is only for the effect of returning objects.

5.1 s.replace (old, new[, Count]) string

  

  

It can be seen that the original string itself is constant, just return a new object, this object in memory, if you want to take advantage of the new object, you can assign it to the variable, of course, can also be used as the parameters of the function directly to the parameter, but the data type should be noted when you pass the parameter.

Where count specifies the number of times to be replaced, and all is replaced without specifying

7. Alignment and padding in strings, etc.

The action here is similar to editing text in world, so you can associate it with understanding.

7.1 S.ljust (width[, Fillchar]), String

Align Left

First of all to reflect the effect of alignment to have a premise, that is that the line can not be full, otherwise no matter how alignment is not used, and in Python, a string object is the default is a full line, its line width is the string is the length.

However, when I force the declaration of the width of this line, and this width isgreater than the length of the original string (less than the invalid, but not an error), the alignment is effective at this point.

    

In order to reflect the effect I put the cursor up, otherwise it is not a space to see.

We set the width of the line to 15, and the original string I use the len () built-in function to derive its length, I know the original string length is 11. When I specify a number of 15, and set the left alignment, the right side of the extra position is filled by default with a space, of course, we can also specify what padding (fillchar), but only the string type.

    Note that only one character can be used and cannot be in Chinese.

7.2 s.rjust (width[, Fillchar]), String

Right alignment, same principle

7.3 S.center (width[, Fillchar]), String

Center alignment, the same principle, but the width specified here is on both sides of the and, and the more out of the position can not be evenly distributed on both sides of the word will take precedence to the right

Set to 15 o'clock, the extra position is 15-11=4, can be divided evenly on both sides, each side is a 4/2=2 position, but set to 16 o'clock, 16-11=5,5 location can not be evenly distributed, at this time priority to the right one more.

7.4 S.zfill (width), string

The quick Fill method is only right-justified, and the content of the fill is 0

7.5 S.strip ([chars]), string or Unicode

In addition to padding, we can also counter-populate, this method is to remove the format is similar to the padding within the string filled with content.

However, this does not mean that we have to use the alignment Fill method to be used later, only the format conforms to it.

    The default is to remove the space on both sides, but note that the middle of the space is not removed .

Of course, we can also specify the characters to be removed

As long as the padding content is the same, it doesn't matter if you specify a padding character, even if it's more than the padding character in the original string.

7.6 S.lstrip ([chars]), string or Unicode

Similar to the above, but this only removes the padding on the left.

7.7 S.rstrip ([chars]), string or Unicode

Similar, only remove the right

8. Separation of strings

The separation of the strings is done by separating the given tokens, returning a new object consisting of the separated string, like a chopping dish, drawing a mark, and a knife falling in the hand, and the dish is divided into paragraphs.

8.1 S.split ([Sep [, Maxsplit]]), List of strings

The Maxsplit method returns a list object, SEQ is the given string, which represents the maximum number of times the separator is separated by default whenever a given delimited string appears.

There are some very strange behavior characteristics, when the two separator character adjacent, the corresponding position of the list will be empty string elements, the maximum number of times 0 is not delimited, the separator character at the end of the string will also appear empty string.

In addition, the separation is left-to-right, is all separated, the direction is not important, the result is the same, but if the maximum number of times is specified, it is meaningful, because the calculation of the number of separators is left-to-right.

8.2 s.rsplit ([Sep [, Maxsplit]]), List of strings

Ditto, except that the separation direction is from right to left.

8.3 s.splitlines (keepends=false), List of strings

Separated by line breaks, note that the line break in Linux is \ n, and the newline character in Windows is \ r \ n

  The above separation is a feature, that is, the specified separator character will not appear in the result of the separation

8.4 s.partition (Sep) (head, Sep, tail)

This separation method returns a Ganso object compared to the above separation method, and is sorted only once, in addition to the following (' delimited ' before ', ' delimited ', ' delimited ', ' separated characters ').

 

In addition, it is the first one to find, from left to right .

8.5 s.rpartition (Sep) (head, Sep, tail)

Ditto, but looking from right to left .

9. Special character Escapes

9.1 S.expandtabs ([Tabsize]), String

Change tab (\ r) to space, default a tab to convert 8 spaces, you can use Tabsize to specify the number of spaces to convert

10. String encoding

10.1 S.decode ([encoding[,errors]]), Object

Decoding

10.2 S.encode ([encoding[,errors]]), Object

Coding

Codec problem will be described in the file operation

11. String concatenation

11.1 S.join (iterable), String

The previous post has been detailed, and it's not repeated here.

12. String formatting

12.1% operator

The previous post has been detailed, and it's not repeated here.

12.2 S.format (*args, **kwargs), String

% formatted version of the upgrade, the general is enough, so I did not know how, here is not nonsense.

13. String filtering

13.1 s.translate (table [, Deletechars]), String

According to the table given in the parameter tables (translation tables, translation tables are converted by the Maketrans method) the characters of the string, the characters to be filtered out into the del parameter.

Python string-built-in method usage analysis

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.