Basic Python Introduction (4) Common operations of strings, comprehensive and easy to understand, simple and practical! ! !

Please read carefully! ! !

Strings are almost the most involved piece of content in the project development process of all programming languages. The basic operations of strings are used in many places. Lists and tuples are mentioned earlier. Let’s talk about strings this time.


Common operations of strings:

1. Concatenate strings

In a string, you can directly use the "+" operator to complete the splicing of the string, and the "+" operator can connect multiple strings and generate a string object.
Example:
Insert picture description here
Operation result:
Insert picture description here
Attention! ! : When implementing string splicing, be sure to splice the string type. Strings are not allowed to be spliced ​​directly with other types. If you need to splice with other types, you can use the str() function for type conversion.
Example:
Insert picture description here
Run results :
Insert picture description here

2. Calculate the length of the string

Sometimes we need to calculate the length of the string. In Python, the len() function is provided to calculate the length of the string. The
syntax format is as follows:

len (string)

Example:
Insert picture description here
running result:
Insert picture description here

3. Intercept string

The method of intercepting a string is actually mentioned above. It is achieved by slicing. The slicing method is similar to lists and tuples. You can refer to sequence slicing to learn.
Example:
Insert picture description here
Run results:
Insert picture description here

4. Split the string

In Python, you can use the split() method of the string object to split the string into a list of strings according to the specified separator. The
syntax format is as follows:

p.split (sep, maxsplit)

str: used to specify the separator
maxsplit: optional parameter, used to specify the number
of splits Example:
Insert picture description here
running results:
Insert picture description here

5. Retrieve the string

In Python, the string object provides many methods for string search. Here are a few commonly used methods
count() method: used to retrieve the number of times a specified string appears in another string, if retrieved If the string does not exist, return 0, otherwise return the number of occurrences. The
syntax format is as follows:

str.count(sub[,start[,end]])

str: the original string
sub: the string to be retrieved
start: optional parameter, indicating the index of the starting position of the retrieval range, if not specified, the retrieval from the beginning
end: optional parameter indicating the index of the ending position of the retrieval range If it is not specified, it will be retrieved to the end.
Example:
Insert picture description here
Run result:
Insert picture description here
find() method: used to retrieve whether the specified substring is included. If the retrieved string does not exist, return -1, otherwise return the first occurrence of the The
syntax of the index in string is as follows:

str.find(sub[,start[,end]])

Example:
Insert picture description here
running result:
Insert picture description here

index() method: similar to the find() method, it is also used to retrieve whether the specified substring is included, but if the index() method is used, an exception will be thrown when the specified string does not exist.
startswith( ) Method: This method is used to retrieve whether the string contains the beginning of the specified substring, if yes, it returns True, otherwise it returns False. The
syntax format is as follows:

str.startswith(sub[,start[,end]])

endswith() method: This method is used to retrieve whether the string contains the end of the specified substring. If yes, it returns True, otherwise it returns False. The
syntax format is as follows:

str.endswith(sub[,start[,end]])

Example:
Insert picture description here
running result:
Insert picture description here

6. Letter size conversion

In Python, the string object provides an upper() method and a lower() method to convert between uppercase and lowercase letters. The former method is to display all strings as lowercase letters, and the latter is to display all strings as lowercase letters
. :
Insert picture description here
Running results:
Insert picture description here

7. Remove spaces and special characters in the string

When entering data, users may unintentionally enter extra spaces or special characters, so is there any way to remove special characters and spaces in the string?
In Python, the strip() method is provided to remove the spaces and special characters on the left and right sides of the string, the lstripr() method is provided to remove the spaces and special characters on the left side of the string, and the rstrip() method is provided to remove the spaces and special characters on the right side of the string. character.
The syntax format is as follows;

str.strip([chars])

The usage of lstrip() and rstrip() is similar, chars is an optional parameter, you can specify the characters to be removed, if not specified, the default is to remove spaces, newline character "\n", tab character "\t", carriage return" \r"
Example:
Insert picture description here
Run result:
Insert picture description here


I hope you can get something after reading it, come on, come on, come on, come on ( ̄︶ ̄)↗ 

Guess you like

Origin blog.csdn.net/My_daily_life/article/details/108877154