In the process of using strings, we more or less need to modify the contents of the string. Although the string is immutable, we can use methods to return a modified copy.
Below we will introduce several commonly used string modification methods, namely lower(), upper(), lstrip(), rstrip (), and strip().
These two methods are similar to the test method in the previous section. The former returns a copy in which all characters in the string are converted to lowercase characters, while the latter returns a copy in which all characters in the string are converted to uppercase. character.
Look at the following example:
>>>my_str='ABCabc'#String containing uppercase and lowercase letters>>>my_str.upper()#All uppercase strings 'ABCABC'>>>my_str.lower()#All lowercase strings 'abcabc' >>>my_str#Look again to see if the original string has changed 'ABCabc'
It must be noted that a copy is returned and the method is used, but the original string is not changed. If we want to obtain a copy, we can rename it or overwrite it directly.
>>>my_str=my_str.upper()#Directly overwrite my_str>>>my_str'ABCABC'
The principle types of these three methods are used to deal with whitespace characters in strings.
1) The lstrip() method returns a copy, with all leading whitespace characters removed from the string in the copy.
2) The rstrip() method will return a copy, and all trailing whitespace characters will be deleted from the string in the copy.
3) The strip() method will return a copy, with all leading and trailing whitespace characters removed from the string in the copy.
Let's first define a string my_str ='ntwww.dotcpp.comtn', and then look at the picture below:
In the figure, the leading character is the blank character in front of the string. The trailing character is the same. The leading character corresponds to the lstrip() method, the trailing character corresponds to the rstrip() method, and strip() corresponds to the combination of the two methods.
Let's take a look at the code:
>>>my_str='ntwww.dotcpp.comtn'>>>my_str.rstrip()#Return a copy with trailing whitespace characters removed'nt>>>my_str.lstrip()# Return a copy of 'www.dotcpp.comtn' with leading whitespace characters removed>>>my_str.strip()#Return a copy of 'www.dotcpp.com' with leading and trailing whitespace characters removed
Let’s understand this example based on the code and the picture above. In layman’s terms, the copy returned by the leading part deletes the front part, the copy returned by the trailing part deletes the following part, and the copy returned by the strip() method deletes the leading part and the trailing part together.
When we write a program, we sometimes need to search for elements in a string. In addition to the find() method and index() method mentioned earlier, we sometimes also need to use the endswith() method and startswith() method.
Its syntax format is:
my_str.endswith(substring)
my_str is the string name, and the substring parameter is a string. If the end of the string is substing, then True is returned, otherwise False is returned.
Its syntax format is:
my_str.startswith(substring)
my_str is the string name, and the substring parameter is a string. If substing is at the beginning of the string, True is returned, otherwise False is returned.
For example:
>>>my_str='www.dotcpp.com'>>>my_str.endswith('.com')True>>>my_str.startswith('www.')True
The replace() method returns a copy, which replaces the characters we need to replace. Its syntax format is:
my_str.replace(old,new)
my_str is the string name, old is the character to be replaced, and new is the character to be replaced.
For example:
>>>my_str='www.dotcpp.com'>>>my_str.replace('w','m')'mmm.dotcpp.com'
When this method is replaced, it is still a copy returned. It will replace all the characters that need to be replaced, leaving the original string unchanged.