Python programming - special methods for string processing

learning target:

  1. Learn how to create strings
  2. Use the len, min, and max functions to get the length of a string, the largest and smallest characters in a string
  3. Use the subscript operator ([]) to access elements in a string
  4. Use the truncation operator str[start:end] to get a substring from a longer string
  5. Use the + operator to concatenate two strings and the * operator to copy a string
  6. Use the in and not in operators to determine whether a string is contained in another string
  7. Strings are compared using comparison operators (==, !=, <, <=, >, >=)
  8. Iterate over characters in a string using a for loop
  9. Test strings using the methods isalnum, isalpha, isdigit, isidentifier, islower, isupper, and isspace
  10. Search for substrings using the methods endswith, startswith, find, rfind, and count
  11. Convert strings using the methods capitalize, lower, upper, title, swapcase and replace
  12. Remove spaces from the left or right side of a string using the methods lstrip, rstrip, and strip
  13. Format strings using the methods center, ljust, rjust, and format
  14. Apply strings during development of applications (CheckPalindrome, HexToDecimalConversion)
  15. Define special methods for operators
  16. Design Rational Class to Represent Rational Numbers

I. Introduction

        Key Point: This chapter focuses on class design, using the str class in Python as an example and exploring the role of special methods in Python.

        The previous article introduced some important concepts about classes and objects. We have learned how to define a class and how to create and use objects. The str class is not only useful for representing strings, but it is also a good example of class design. In this article we will discuss the str class in more depth. Special methods play a very important role in Python language. Some special methods and operator overloading will also be introduced here, as well as designing classes using special methods.

Two, str class

Key point: a str object is immutable; that is, once the string is created, its contents are immutable.

2.1, create a string

You can build a string using a constructor like this:

s1 = str() # Create an empty string object
s2 = str("Welcome") # Create a string object for We lcome

Python provides a simple syntax for creating a string by using string values. For example:

s1 = " "# Same as s1= str()
s2 = "Welcome" # Same as s2 = str("Welcome")

        A string object is immutable: once a string object is created, its contents cannot change. To optimize performance, Python uses an object to represent strings with the same content. As shown in the figure, both s1 and s2 point to the same string object, and they both have the same id number.

        This action is true for all immutable objects in the Python library. For example: int is an immutable class. Two int objects with the same value actually share the same object, as shown in the figure:

2.2. Functions for processing strings

        Some of Python's built-in functions can be used with strings. You can use the len function to return the number of characters in a string, while the max and min functions return the largest and smallest characters in a string. Here are some examples:

>>> S = "We1come"
>>> len(s)
7
>>> max(s)
'o'
>>> min(s)
'W'
>>>

        Because string s has 7 characters, len(s) returns 7. Note: The ASCII code value of lowercase letters is higher than the ASCII code value of lowercase letters, so max(s) returns o, and min(s) returns W.

2.3. Subscript operator [ ]

A string is a sequence of characters. You can access a character in a string using the subscript operator using the following syntax:

s[index]

Subscripts are 0-based; that is, they range from 0 to len(s)-1, as shown.

2.4. Interception operator [start : end]

        The interception operator returns a segment of a string using the syntax s[start:end]. This section is a substring from subscript start to subscript end-1. For example:

>>> s = "Welcome"
>>> s[1 : 4]
elc

s[1:4] returns the substring from index 1 to index 3.

        Either the starting or ending subscript can be ignored. In this case, the default starting index is 0, and the ending index is the last index. For example:

>>> S = "Welcome"
>>>s[ :6]
'Welcom'
>>> s[4: ]
'ome'
>>>s[1:-1]
elcom
>>>

        In line 2, s[:6] and s[0:6] are the same, and both return substrings from index 0 to index 5. In line 4, s[4: ] and s[4:7] are the same, and both return substrings from subscript 4 to subscript 6. You can also use negative subscripts when intercepting strings. For example, in line 6, s[1:-1] and s[1:-1+len(s)] are the same .

        Note: If the subscript (i or j) in the interception operation s[i:j] is a negative number, then use len(s)+index to replace the subscript. If j>len(s), then j will be set to len(s). If i>=j, then the intercepted substring will become an empty string.

2.5, concatenation operator + and copy operator *

        You can combine or concatenate two strings using the concatenation operator +. You can also use the copy operator * to concatenate the same string multiple times. Here are some examples:

>>> s1 = "Welcome"
>>> s2 = "Python"
>>> s3 = s1 + "to" + s2
>>> s3
'Welcome to Python'
>>> s4 = 3 * s1
>>> s4
'WelcomeWelcomeWelcome'
>>> s5 = s1 * 3
>>>s5
'WelcomeWelcomeWelcome'
>>>

Note: 3*s1 and s1*3 have the same effect.

2.6, in and notin operators

You can use the in and not in operations to test whether a string is within another string. Here are some examples:

>>> s1 = "Welcome" 
>>> "come" in s1
True
>>> "come" not in s1
False
>>>

Here's another example:

s = input("Enter a string: ")
if "Python" in s:
    print("Python","is in", s)
else:
    print("Python","is not in", s)

        If you run this program with the string "Welcome toPython" as input, the program should display: python is in Welcome to Python.
        

2.7. Comparing strings

        You can compare strings using comparison operators (==, !=, >, <. >=, and <= have been introduced). Python performs comparisons by comparing corresponding characters in strings. The comparison is implemented by calculating the numerical code of the characters. For example, a is greater than A because the numeric code of a is greater than the numeric code of A.

        Suppose you need to compare the strings s1("Jane") and s2("Jake"). First, compare the first characters (J and J) in s1 and s2. Since they are the same, compare their second characters (a and a). Since they are also the same, compare their third characters (n and k). Because the ASCII code value of n is greater than k, s1 is greater than s2.

        Here are some examples:

>>>'green' == 'glow'
False
>>> "green" != "g1ow"
True
>>> "green" > "glow"
True
>>> "green" >= "glow"
True
>>> "green" < "g1ow"
False
>>> "green" <= "g1ow"
False
>>> "ab" <= "abc"
True
>>>

2.8. Iterate strings

        A string is iterable. This means you can use a for loop to sequentially iterate through all the characters in a string. For example, the following code displays all characters in the string s:

for ch in s:
    print(ch)

You can read this code as "for every character ch in s, print ch".

        This for loop does not use subscripts to access characters. However, if you want to iterate over the characters in a different order, then you still have to use subscripts. For example, the following code displays characters in odd positions of a string:

for i in range(0, len(s), 2):
    print(s[i])

        This code uses the variable i as the subscript of the string s. The initial value of i is 0 and then increases by 2 each time until it reaches or exceeds len(s). For each value of i, print s[i].

2.9. Test string

2.10. Search string

2.11. Convert strings

 Note: As mentioned before, strings are immutable. There are no methods in the str class that can change the content of the string. These methods create new strings.

2.12. Delete spaces in strings

        You can use the method in the figure below to remove spaces in a string from the front, end, or both ends of the string. To
review, the characters ' ', \t, \f, \r, and \n are all called whitespace characters.

        Note: The method of deleting whitespace characters can only remove the whitespace characters at the front and rear ends of the string, and cannot delete the whitespace characters surrounded by non-whitespace characters.

        Tip: It is a good rule of thumb to apply the strip() method on the input string to ensure that any unwanted characters at the end of the input are removed.

2.13. Format string


 Here are some examples of using the center, ljust and rjust methods:

>>> s = "Welcome"
>>> s1 = s.center(11)
>>>s1
'  Welcome  '
>>> s2 = s.ljust(11)
>>>>s2
'Welcome       '
>>> s3 = s.rjust(11)
>>;>>s3
'      Welcome '
>>>

        On line 2, s.center(11) places the string s in the center of the 11-character string. On line 5, s.ljust(11) places the string s at the left end of the 11-character string. On line 8, s.rjust(11) places the string s at the right end of the 11-character string.

3. Operator overloading and special methods

Key Point: Python allows you to define special methods for operators and functions to implement common operations. Python has a unique way of naming these methods to identify their relatedness.

        Previously, we have learned how to use operators to perform string operations. You can use the operator + to combine two strings, and the operator * to combine the same string multiple times. Relational operators (==, !=.<.<=, >, >=) are used to compare two characters string, and the subscript operator [] is used to access a character. For example:

s1 ="Washi ngton"
s2 = "Cali fornia"
print("The first character in s1 is",s1[0] )
print("s1+s2is",s1+s2)
print("s1 < s2?",s1 < s2 )

        These operators are practical. The above are methods defined in the str class. Defining methods for operators is called operator overloading. Operator overloading allows programmers to use built-in operators to define methods for users. The following table lists the mapping between operators and methods. When you name these methods, add two underscores before and after the name so that Python can identify their relevance. For example: To use operator + as a method, you should define a method named __add__. Note: These methods are not private because they have two trailing underscores in addition to the two starting underscores. To recap, initializers in classes are named __init__ , which is a special method for initializing objects.

For example, you can rewrite the previous code as follows:

s1 = "Washington"
s2 = "California"
print("The first character in sl is", s1.__ getitem__(0) )
print("s1 + s2 is", s1.__add__ (s2))
print("s1 < s2?", s1.__lt_ __ (s2))

        s1.__ getitem__ (0) is the same as s1[0], sl.__ add__ (s2) is the same as s1+s2, and s1.__ lt__ (s2) is the same as s1<s2. Now, you can see the advantages of overloaded operators. Overloading operators can greatly simplify programs and make them more readable and maintainable.

        Python supports the in operator, which can be used to determine whether a character is in another string or whether an element is a member of a container. The corresponding method is named __contains__(self,e). You can use the method __contain__ or use the in operator to determine whether a character is within a string. The code is as follows.

1 s1 = "Washi ngton"
2 print("Is W in s1?", 'W' in s1 )
3 print("Is W in s1?", s1.__contains__('W') )

W in s1 is the same as s1.__ contains__('w').

If a class defines a __len__ (self) method, then Python allows you to call the method as a function using a convenient syntax. For example: The __len__ method is defined in the str class. This method returns the number of characters in a string. You can either use the __len__ method or the len function to get the number of characters in a string. The code is as follows:

1 s1 = "Washington"
2 print("The 1ength of s1 is", 1en(s1))
3 print("The 1ength of s1 is", s1.__1en__())

len(s1) and s1.__ len__ () are the same.

        Many special operators are defined as Python's built-in types, such as int and float. Suppose i is 3 and j is 4. i.__add__ (j) is the same as i+j, and i.__sub__(j) is the same as ij.

Note: You can pass an object to call print(x). This is equivalent to calling print(x.__ str__()) or print(str(x)).

        Note: Comparison operators (==, !=, <, <=, >, and >=) can also be implemented using the method __cmp__ (self,other). If self<other, then this method returns a negative integer. If self==other, then this method returns 0. If self>other, then this method returns a positive integer. For two objects a and b, if __It___ is available, then a<b calls a.__It__(b). If that doesn't work, call the __cmp__ method to determine the order.

4. Summary

  • A string object is immutable and its contents cannot be changed.
  • You can use the Python functions len, min, and max to return the length, maximum element, and minimum element of a string.
  • You can use the subscript operator [] to point to a single character in a string.
  • You can use the concatenation operator + to concatenate two strings, the copy operator * to copy a string multiple times, the truncation operator [: ] to obtain a substring, and the operators in and not in to determine - a Whether the character is in a string.
  • Compare two strings using comparison operators (==, !=, <, <=, >, and >=).
  • Use a for loop to iterate over all characters in the string.
  • Functions like endswith, startsswitch, isalpha, islower, isupper, lower, upper, find, count, replace, and strip can be used on string objects.
  • Special methods can be defined for overloaded operations.

Guess you like

Origin blog.csdn.net/java_faep/article/details/132302679