1. substring
- str.replace() : find and replace
fav_color = "red is my favorite color"
fav_color = fav_color.replace("red", "blue")
print(fav_color)
输出:
blue is my favorite color


2. str.title():returns a copy of the string with the first letter of each word transformed to uppercase 
3. 用函数去掉字符串中不需要的符号
test_data = ["1912", "1929", "1913-1923",
"(1951)", "1994", "1934",
"c. 1915", "1995", "c. 1912",
"(1988)", "2002", "1957-1959",
"c. 1955.", "c. 1970's",
"C. 1990-1999"]
bad_chars = ["(",")","c","C",".","s","'", " "]#需要删除的字符
bad_chars = ["(",")","c","C",".","s","'", " "]
def strip_characters(string):
for char in bad_chars:
string = string.replace(char,"")
return string
stripped_test_data = []
for d in test_data:
date = strip_characters(d)
stripped_test_data.append(date)
-
str.split(): split a CSV from one single string into a list of strings and then into a lists of lists.

-

-
str.format(): inserting values into strings.
→→→
并且convert string from integer. 或者用key arguments

-
str.startswith()检查开头字符是否为我们要找的

-

本文介绍了Python中字符串处理的实用技巧,包括替换子串、转换大小写、去除特殊字符及字符串分割等,适合初学者掌握基本的字符串操作方法。
5449

被折叠的 条评论
为什么被折叠?



