Python随笔6

字符串方法:

find方法:

def find(self, sub, start=None, end=None)
S.find(sub[, start[, end]]) -> int
find()方法的作用是查找子串在字符串中第一次出现的最左端索引,返回的是索引的位置;查找为空返回-1;子串作为第一个参数必须有;也可以查找从star开始到end结束的范围内的子串
例子:
str="I think Python is Good"
#只有一个参数子串
print("#只有一个参数子串")
print(str.find("Python"))
#有子串和start两个参数
print("#有子串和start两个参数")
print(str.find("Python",2))
#有子串和start、end三个参数
print("#有子串和start、end三个参数")
print(str.find("Python",2,10))
print(str.find("Python",2,15))

结果:

#只有一个参数子串
8
#有子串和start两个参数
8
#有子串和start、end三个参数
-1
8

join()方法:

def join(self, iterable)
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.
S字符串对象,用来连接序列中的元素,返回一个字符串对象;如果S="+"则表示用+来连接,如果S="/",则表示用/来连接;如果S="\\",则表示用\来连接(转义字符)
例子:
str=["Allen","Bob","Tom","Jerry"]
print("+".join(str))
print("-------这里想要输出Linux的路径,特地在str2中加了第一个元素""------------")
str2=("","home","users")
print("/".join(str2))
print("---------输出Windows下的路径")
print("c:"+"\\".join(str2))

结果:

Allen+Bob+Tom+Jerry
-------这里想要输出Linux的路径,特地在str2中加了第一个元素------------
/home/users
---------输出Windows下的路径
c:\home\users

split()方法:

def split(self, sep=None, maxsplit=-1): 
"""
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
"""
return []
split()为字符串S切片,sep默认为空格但不能为"";maxsplit表示切割的次数,最终将字符串切割为maxsplit+1个元素返回到列表中;如果sep为空格或者未指定时,字符串S中的空格删除掉;如果
sep为字符串S中的某个字符,那么与sep相等的字符会被删除掉。
例子:
str="Tom+Jerry+Bob"
print(str.split("+",3))
print(str.split("+"))
str1="Tom Jerry Bob"
print(str1.split())
str2="i like live in downtown"
print(str2.split("i"))

结果:

['Tom', 'Jerry', 'Bob']
['Tom', 'Jerry', 'Bob']
['Tom', 'Jerry', 'Bob']
['', ' l', 'ke l', 've ', 'n downtown']

lower()方法:

返回字符串的小写字母版

例子:

name="TOM"
names=["tom","jerry","sam"]
if name.lower() in names:
   print(True)
else:
    print(False)

结果:

True

replace()方法:

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
"""
return ""
replace()方法是用参数new来替换字符串S中的old字符,如果不指定count,默认全部替换,指定count之后替换count个
例子:
hobby = "I like Apple Apple Apple"
print("#不指定count")
print(hobby.replace("Apple","Pears"))
print("#指定count")
print(hobby.replace("Apple","Pears",1))
print(hobby.replace("Apple","Pears",3))

结果:

#不指定count
I like Pears Pears Pears
#指定count
I like Pears Apple Apple
I like Pears Pears Pears

strip()方法:

def strip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""
默认去除字符串两端的空格,指定参数之后,就去除字符串两端指定的参数
例子:
hobby = "i like living in downtown "
print("#不指定字符,默认去除字符串两端的空格")
print(hobby.strip())
print("#指定字符,去除字符串两端的指定字符")
print(hobby.strip("i"))

结果:

#不指定字符,默认去除字符串两端的空格
i like living in downtown
#指定字符,去除字符串两端的指定字符
 like living in downtown 

translate()方法和maketrans()方法:

maketrans()方法,建立映射关系,返回的是字典;如果有两个参数,第一个参数是字符串中要替换的值(字符串中没有的不做替换),第二个参数是替换之后的值,两个参数的长度必须相等;第三个参数是删除字符串中的值(字符串中没有的话不做删除),第三个参数可选

配合translate()方法,可以将替换之后的字典输出为字符串

s="abcdefg-123456"
#映射为空
table=s.maketrans("","")
print(s)
print(table)
print(s.translate(table))
#有两个参数
table=s.maketrans("abc123","ABC789")
print(s)
print(table)
print(s.translate(table))
#有三个参数
table=s.maketrans("abc123","ABC789","abc")
print(table)
print(s.translate(table))

结果:

abcdefg-123456
{}
abcdefg-123456
abcdefg-123456
{97: 65, 98: 66, 99: 67, 49: 55, 50: 56, 51: 57}
ABCdefg-789456
{97: None, 98: None, 99: None, 49: 55, 50: 56, 51: 57}
defg-789456

 

posted @ 2018-11-11 13:32  bigbigtong  阅读(288)  评论(0编辑  收藏  举报