Summary of functions and methods of commonly used operation strings in Python, and python strings

Source: Internet
Author: User
Tags string methods

Summary of functions and methods of commonly used operation strings in Python, and python strings

For example, a Python string contains several characters: P, y, t, h, o, and n. This sort is very strict, not only the characters themselves, but also the order. In other words, if a character is changed, a new string will be programmed. If the order of these characters changes, it also becomes a new string.

In Python, object types such as strings (other similar object types with such characteristics, such as lists) are collectively referred to as sequences. As the name implies, a sequence is "ordered ".

For example, the first hero in shuibo Liangshan (there are also clearly female in it. Do female come from here ?), It is a "ordered" sequence. From Song Jiang, the boss, we have been arranging for 108th Jinmao dogs. In this sequence, each person has a serial number, which corresponds to each person one by one. The first is Song Jiang, and the second is Lu Junyi. In turn, each person's name can be used to identify the corresponding number. What is the number of Wu Song? 14. What about Li Yan? 22nd.

In Python, the numbers are given an elegant name called index (this is also called by other programming languages, which is not unique to Python .).

Indexing and Slicing
We used the example of Liangshan haohan to illustrate the index. Let's look at the example in Python:

>>> lang = "study Python">>> lang[0]'s'>>> lang[1]'t'

There is a string that is assigned to the variable lang through the value assignment statement. To obtain the first word s of this string, use lang [0]. Of course, if you do not want to use the value assignment statement to point the variable lang to that string, you can also do this:

>>> "study Python"[0]'s'

The results are the same. Because lang is a tag, it points to the "study Python" string. When you want Python to execute lang [0], it is to convert it to the string object, just like the above operation. However, if you don't need a variable like lang, if you want to write it later, you will have to write the string completely every time. To save trouble, copy it to a variable. A variable represents a string.

The sorting method of the string sequence is a little different from that of Liangshan haohan. The first one is represented by a number 0 instead of a number 1. Not only Python, but many other languages are sorted from 0. Why? This is the rule. Of course, this rule has some advantages. This is not shown here. If you are interested, go to google and have an article specifically explaining this.

0 1 2 3 4 5 6 7 8 9 10 11
S T U D Y L P Y T H O N


In the preceding table, the string is sorted from the first to the last one. Note that the space in the middle of the two words also occupies a position.

The index can be used to find the character corresponding to the index. In turn, can we use the character to find its index value in the string? How to find it?

>>> lang.index("p")6

In this way, is it possible to match the example of a hero in Liangshan? The difference is that the first index value is 0.

If one day, brother Song stood on the big stone and shouted to his brothers, "you have lined up ." After waiting for the brothers to arrange, Song Jiang said: "Now, I have assigned my friends who have no wives. I already have a list, and my friends will come up. But I read it by serial number. From 29th to 34th, go out first, and wait for a girlfriend to be assigned to the house next to him ."

In the previous example, lang [1] can obtain the second character t of the original string, which is equivalent to "cutting" the original string. However, this "cut" does not affect the integrity of the original string. Of course, it can be understood as assigning a value for that character t.

Brother Song did not "cut" one by one, but called out several brothers. You can also do similar things in Python.

>>> Lang 'Study python' # after a number of characters are cut in front of the string, check whether the string is complete. >>> Lang [2: 9] 'udy pyt'

To obtain partial (not one) characters through lang [2: 9], we can see from the returned results that the numbers correspond to 2, 3, 4, 5, 6, 7, 8 (corresponding to the table above) characters (including the space ). That is to say, in this method of getting some characters, you can get the characters that are required at the beginning and before the last sequence number. You can check the number of tables in the table above. In short, it includes the beginning, not the end.

The process of getting characters through indexes, whether it is one or more, is called a slice.

Slice is a very interesting thing. Can we cut a lot of tricks?

>>> Lang 'Study python' >>> B = lang [1:] # obtain the character from Number 1 to the end, in this case, you do not need to write >>> B 'tudy python' >>> c = lang [:] # Get all the characters >>> c 'Study python' >>> d = lang [: 10] # Get the character from the first to the 10th> d 'Study pyth'

When obtaining a slice, if the serial number before or after the semicolon is not written, it indicates that it is the last (not written after) or the first (not written before)

Lang [: 10] has the same effect as lang [0: 10.

>>> e = lang[0:10]>>> e'study pyth'

So, is lang [1:] the same effect as lang? Please answer your question after thinking.

>>> lang[1:11]'tudy pytho'>>> lang[1:]'tudy python'

Really different. Are you right? The reason is as mentioned above. If a semicolon is followed by a number, the slice obtained does not contain the serial number corresponding to the number (included first, not later ). So, is it possible? Lang [] does not include the 12th (actually there is no 12th). Can I get the characters corresponding to the 1st to 11th?

>>> lang[1:12]'tudy python'>>> lang[1:13]'tudy python'

Sure enough. Besides writing 12 and 13, you can get the same result. However, I would like to remind you that this method of obtaining slices is not recommended in programming practice. Especially if the loop is to be used later, it may be difficult to do so.

If no number is written between the left and right sides of the colon during slicing, It is the c = lang [:] in the previous operation. The result is that the value of variable c is the same as that of the original string, that is, copy a copy. Note: The "copy" here is enclosed in quotation marks, which means it is like copying. Is it true? You can use the following method to verify

>>> id(c)3071934536L>>> id(lang)3071934536L

Id () is used to view the object's memory address (that is, the location number in the memory ). From the above, we can see that the two memory addresses are the same, indicating that the c and lang variables point to the same object. The c = lang [:] method does not generate a new string. Instead, the variable c label is pasted on the original string.

>>> lang = "study python">>> c = lang

In this case, does variable c and lang point to the same object? Or what is the object memory address that the two point? The viewer can view it on his own.

Basic string operations
A string is a sequence. All sequences have the following basic operations:

  • Len (): calculates the sequence length.
  • : Connect two sequences
  • : Repeated sequence element
  • In: determines whether an element exists in a sequence.
  • Max (): returns the maximum value.
  • Min (): returns the minimum value.
  • Cmp (str1, str2): compare whether the two sequence values are the same

The following example shows how to use these basic operations on strings:

"+" Connection string

>>> str1 + str2'abcdabcde'>>> str1 + "-->" + str2'abcd-->abcde'

This is actually splicing, but here, the viewer should have a larger idea. Now we just learned the string sequence, and we will see the list and tuples in the future, can be spliced in this way.

In

>>> "a" in str1True>>> "de" in str1False>>> "de" in str2True

In is used to determine whether a string is in another string, or whether a string contains a string. if it contains a string, True is returned. Otherwise, False is returned.

Maximum Value

>>> max(str1)'d'>>> max(str2)'e'>>> min(str1)'a'

Each character in a string is encoded in the computer, that is, it corresponds to a number. min () and max () are obtained based on the minimum and maximum values of this number, the corresponding characters are displayed. For details about the number, you can refer to google for character encoding or ASCII encoding, which is easy to find.

Comparison

>>> cmp(str1, str2)-1

Compare the two strings. First, convert the symbols in the string to a number and then compare them. If the returned value is less than zero, it indicates that the first value is smaller than the second value and equal to 0, the two values are equal, greater than 0, and the first value is greater than the second value. In order to understand why, go to the following analysis.

>>> ord('a')97>>> ord('b')98>>> ord(' ')32

Ord () is a built-in function that returns an ASCII value (decimal) of a character (Note: it is a character, not a string consisting of multiple characters ), the value of character a in ASCII is 97, and the space also has a value in ASCII, Which is 32. By the way, you can use chr ():

>>> chr(97)'a'>>> chr(98)'b'

The following comparison result is displayed:

>>> Cmp ("a", "B") # a --> 97, B --> 98, 97 less than 98, so a is less than B-1> cmp ("abc", "aaa") 1> cmp ("a", "a") 0

Let's take a look at the comparison below. How is it done?

>>> cmp("ad","c")-1

In string comparison, the first character of the two strings is compared first. If they are equal, the next character is compared. If they are not equal, the result is returned. Until the end, if it is equal, 0 is returned. When the number of digits is not enough, the processing is not performed (note that there is no NUL corresponding to 0, 0 in ASCII), and the one with Multiple Digits is naturally large. In ad, a is compared with c. Obviously, a is smaller than c, and result-1 is returned. The following comparison is the most confusing one. Can I stay away from understanding what I just described?

>>> Cmp ("123", "23")-1 >>> cmp (123,23) # can also compare integers. This is the direct comparison of integers. 1 "*"

"Multiplication" in the string. This multiplication refers to repeating the meaning of the string. It is useful in some cases. For example, I want to print a gorgeous split line:

>>> Str1 * 3 'abcdabcdabcd'> print "-" * 20 # Do not enter many '-' ------------------ len ()

You need to know the number of characters in a string. One way is to stare at the screen from the beginning. Oh, this is not a computer, but a key customer.

Key customer, not a swordsman. A swordsman uses a sword as a weapon, while a key hacker uses a keyboard as a weapon. Of course, there are also cheap customers, that is the highest level of the bitch, to the level of the hero, such as the stream of Yue weiqun.
As a key customer, the string length is as follows:

>>> a="hello">>> len(a)5

The len (object) function is used ). The result is the length of the string.

>>> M = len (a) # assign the returned result to a variable >>> m5 >>> type (m) # Return the value (variable) is an integer <type 'int'>

String formatting output
What is formatting? There are special entries in Wikipedia:

Formatting refers to the initialization of partitions in a disk or disk. This operation usually causes all files in an existing disk or partition to be cleared.
I wonder if you know this "formatting ". Obviously, this formatting is not what we are talking about here. We are talking about string formatting, or formatting a string, which means:

A formatted string is a string parameter used in C, C ++, and other programming languages printf to specify the format and relative position of output parameters. The conversion description (conversion specification) is used to convert the corresponding 0 or multiple function parameters to the corresponding format output, while the conversion description of other characters in the formatted string is output as is.
This is also the definition from Wikipedia. In this definition, the C language is used as an example and its output function is used for illustration. In Python, the same operation and similar function print are also available. We have learned about this before.

If the definition is more popular and the string is formatted, you must first create a template, set aside space in one or more places in the template, and then fill in the strings in those spaces. Then, those spaces need to be represented by a symbol, which is usually called a placeholder (only occupying that position, not the output content ).

>>> "I like %s"'I like %s'

In this string, there is a symbol: % s, which is a placeholder, which can be replaced by other strings. For example:

>>> "I like %s" % "python"'I like python'>>> "I like %s" % "Pascal"'I like Pascal'

This is a commonly used string output method.

In addition, different placeholders indicate that the position should be filled with different types of objects. Many of them are listed below for your reference. However, you do not need to remember. Only % s and % d are commonly used, or % f is added. If you need other items, click here to check them.

Placeholder Description
% 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 Exponent (base write as e)
% E Exponent (base write as E)
% F Floating Point Number
% F Floating Point Number, same as the top
% G Exponent (e) or floating point number (based on the display length)
% G Exponent (E) or floating point number (based on the display length)


Example:

>>> a = "%d years" % 15>>> print a15 years

Of course, you can also set multiple placeholders in a string, just as below

>>> print "Suzhou is more than %d years. %s lives in here." % (2500, "qiwsir")Suzhou is more than 2500 years. qiwsir lives in here.

You can also specify the number of decimal places and other formats for the output of floating point numbers.

>>> print "Today's temperature is %.2f" % 12.235Today's temperature is 12.23>>> print "Today's temperature is %+.2f" % 12.235Today's temperature is +12.23

Note: In the above example, the rounding operation is not implemented. Only intercept.

Common string Methods
There are many string methods. You can use dir to view:

>>> dir(str)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

So much will not be described one by one. To understand a specific meaning and usage, it is best to use help to view it. Example:

>>> help(str.isalpha)
Help on method_descriptor:isalpha(...)  S.isalpha() -> bool  Return True if all characters in S are alphabetic  and there is at least one character in S, False otherwise.

Follow the instructions here to conduct an experiment in interactive mode.

>>> "Python". isalpha () # The string is full of letters, and TrueTrue should be returned >>> "2 python". isalpha () # The string contains non-letters, and FalseFalse is returned

Split

This function is used to split a string based on a delimiter.

>>> a = "I LOVE PYTHON">>> a.split(" ")['I', 'LOVE', 'PYTHON']

This is separated by space. A returned value named list is obtained. The list content will be described later. Can I use other separators?

>>> b = "www.itdiffer.com">>> b.split(".")['www', 'itdiffer', 'com']

Removes spaces at both ends of the string.

This function is useful for users to enter some information. Some friends like to press a space when the input ends, such as asking him to enter his own name. When the input ends, he gives a space. Some users prefer to add a space first. The first character in the input should have two spaces in front of it.

These spaces are useless. Python helps programmers remove these spaces because many people may have this habit.

The method is:

  1. S. strip () removes the Left and Right spaces of the string
  2. S. lstrip () removes the left space of the string
  3. S. rstrip () removes the right space of the string
  4. For example:
>>> B = "hello" # spaces on both sides >>> B. strip () 'hello' >>> B 'hello'

Note that the original value is not changed, but a new result is returned.

>>> B. lstrip () # Remove the left space 'hello'> B. rstrip () # Remove the right space 'hello'

Case-insensitive Conversion

For English, case-sensitive conversion is sometimes required. The most famous camper name, which contains uppercase and lowercase parameters. If you are interested, you can refer to the method for automatically converting a string into a camper name.

There are a bunch of built-in functions in Python to implement case-insensitive conversion of various types.

  • S. upper () # uppercase letters in S
  • S. lower () # lowercase letters in S
  • S. capitalize () # uppercase letters
  • S. isupper () # Are all uppercase letters in S?
  • S. islower () # Are all lowercase letters in S?
  • S. istitle ()
  • Example:
>>> A = "qiwsir, Python" >>>. upper () # convert lowercase letters into uppercase letters 'qiwsir, python' >>> a # the original data object has not changed 'qiwsir, python' >>> B =. upper () >>> B 'QIWSIR, python' >>> c = B. lower () # convert all lowercase letters into uppercase letters> c 'qiwsir, python'> a 'qiwsir, python'>. capitalize () # change the first letter of the string to 'qiwsir, python' >>> a # the original data object has not changed 'qiwsir, python' >>> B =. capitalize () # a New One> B 'Qiwsir, python'> a = "qiwsir, github" # The problem here is pointed out by white feather, a netizen, thank you very much. >>> A. istitle () False >>> a = "QIWSIR" # returns False if all values are uppercase >>>. istitle () False >>> a = "qIWSIR" >>>. istitle () False >>> a = "Qiwsir, github" # If so, False >>>. istitle () False >>> a = "Qiwsir" # This is True >>>. istitle () True >>> a = 'qiwsir, Github '# this is also True >>>. istitle () True >>> a = "Qiwsir" >>>. isupper () False >>>. upper (). isupper () True >>>. islower () False >>>. lower (). islower () True

You can do this again:

>>> A = "This is a Book" >>>. istitle () False >>> B =. title () # convert the first letter of all words to uppercase >>> B 'This Is A Book >>> B. istitle () # determines whether the first letter of each word is capitalized. True

Join string

"+" Can be used to concatenate strings, but it is not always possible. For example, you can splice each character (string) element in the list (for details about the list, it is another type) into a string and connect it with a specific symbol, if "+" is used, it will be more troublesome (it can be implemented and troublesome ).

String join is easier to implement.

>>> b'www.itdiffer.com'>>> c = b.split(".")>>> c['www', 'itdiffer', 'com']>>> ".".join(c)'www.itdiffer.com'>>> "*".join(c)'www*itdiffer*com'


Is this splicing simple?

Articles you may be interested in:
  • Summary of commonly used string-related functions in python
  • Summary of Python string processing functions
  • Common Function Analysis for Python string and file operations
  • How to format strings using the format function in Python
  • How to Use the startswith () function in Python to determine the start of a string
  • Summary of methods for connecting strings in python
  • How to clear non-letter characters in a string using python
  • How to clear non-numeric characters in a string using python
  • How to save a string to a file in python
  • Three methods for merging strings in Python

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.