Article Directory
foreword
This article mainly introduces simple operations on strings to convert strings into strings in the format we need. For example, operations such as removing redundant characters, adding characters, replacing characters, judging characters, etc.
2. Use steps
1. Delete characters
Create a string "adbhjkkkssss" and delete unwanted characters by using the strip() method. Here simply delete the 's' character, and print out the unprocessed characters and the processed characters for comparison. The code example is as follows:
teststr = 'adbhjkkkssss'
print(teststr)
resultstr = teststr.strip('s')
print(resultstr)
After running the script, you can see that the 's' character in the original string has been deleted. The code results are as follows:
2. Add characters
Still take the code just now as an example, use the splicing method to add new characters after the original string, 'original character' + 'new character'. The code example is as follows:
teststr = 'adbhjkkkssss'
print(teststr)
resultstr = teststr + 'hello'
print(resultstr)
After running the script, you can see that new characters are added after the original characters, and the code running results are as follows:
3. Replace characters
Use the replace() of the string to replace the specified character, or take the example just now, replace the 'a' in 'adbhjkkkssss' with 'l'. The code example is as follows:
teststr = 'adbhjkkkssss'
print(teststr)
resultstr = teststr.replace('a','l')
print(resultstr)
After running the script, you can see that the 'a' character is replaced by the 'l' character, and the result of running the code is as follows:
4. Change uppercase characters to lowercase characters
Use the lower() command in the string to convert all uppercase characters in the string to lowercase characters. The code example is as follows:
teststr = 'ADBHJKKKSSSS'
print(teststr)
resultstr = teststr.lower()
print(resultstr)
After running the script, you can see that all uppercase characters become lowercase, and the running results are as follows:
5. Change lowercase characters to uppercase characters
Use the upper() command in the string to convert all lowercase characters in the string to uppercase characters. The code example is as follows:
teststr = 'adbhjkkkssss'
print(teststr)
resultstr = teststr.upper()
print(resultstr)
After running the script, you can see that all lowercase characters become uppercase characters, and the running results are as follows:
6. Judgment of characters, whether it is uppercase, whether it is lowercase, whether it is a number, etc.
Use isnumeric(), isupper(), and islower() in the string to determine whether it is uppercase, lowercase, or a number. The code example is as follows:
teststr = 'adbhjkkkssss'
print(teststr)
resultstr = teststr.isnumeric()
resultstr1 = teststr.isupper()
resultstr2 = teststr.islower()
print(resultstr)
print(resultstr1)
print(resultstr2)
After running the script, you can see the output results. The running results are as follows:
Of course, there are other usages of strings. This article only lists a few of the most simple and common methods. Interested partners can search for the usage of strings.
Summarize
This article mainly introduces simple operations on strings to convert strings into strings in the format we need. For example, operations such as removing redundant characters, adding characters, replacing characters, judging characters, etc.