| Operation |
Grammar |
Example |
Results |
| Find sub-string |
find ("str") 1. Returns the leftmost index where the substring is located 2. No return-1 3. You can accept the start and end points parameters, the range left closed right open |
title = "Monty python ' s Flying Circus" 1.title.find ("Monty") title.find ("Python") 2.title.find ("Zircus") 3.title.find ("Circus", 0, #提供起始点和结束点 |
0 6 -1 -1 |
| Adding a sequence |
"Sep". Join (SEQ) Inverse method of 1.split |
1.sep = "+" seq = ["1", "2", "3", "4", "5"] Sep.join (seq) 2.sep = "/" dirs = ["", "usr", "bin", "env"] "Sep.join (dirs) |
"1+2+3+4+5" "/usr/bin/env" |
| Split sequence |
Str.split ("Sep") Inverse method of 1.join |
1. "1+2+3+4+5". Split ("+") 2. "/usr/bin/env". Split ("/") |
["1", "2", "3", "4", "5"] ["", "usr", "bin", "env"] |
| lowercase letters |
Str.lower () |
1. "Trondheim Hammer Dance". Lower () |
"Trondheim Hammer Dance" |
| Capital |
Str.upper () |
1. "Trondheim Hammer Dance". Upper () |
"TRONDHEIM HAMMER DANCE" |
| Title Conversion |
String.capwords () |
1.import string String.capwords ("That's all, folks") |
"That's all, folks" |
| Replace |
Str.replace (old,new) |
1. "This was a test". Replace ("is", "Ezz") |
' Thezz ezz a test ' |
Remove spaces |
Str.strip () 1. Remove both sides of the space (without internal) 2. Specify the characters that need to be removed and use them as parameters |
1. "Internall whitespace is kept". Strip () 2. "* * * * SPAM * for * everyone!!! ". Strip (" *! ") |
"Internall whitespace is kept" "SPAM * for * everyone" |
Str.lstrip () Remove left space |
1. "Internall whitespace is kept". Lstrip () |
"Internall whitespace is kept" |
Str.rstrip () Remove Right space |
1. "Internall whitespace is kept". Rstrip () |
"Internall whitespace is kept" |