常用的字符串方法:
>>> a = "I like python"
>>> a.split() #其作用是将字符串根据某个分割符进行分割。
['I', 'like', 'python']
>>> b = " python "
>>> b.strip() #去掉字符串的左右空格
'python'
>>> d = "python"
>>> d.upper() #将小写字母改成大写
'PYTHON'
>>> d.upper()
'PYTHON'
>>> e = d.upper()
>>> e
'PYTHON'
>>> e.lower() #将大写改为小写
'python'
>>> a.capitalize() #把字符串的第一个字母变成大写
'I like python'
>>> c
['www.itdiffer.com']
>>> c = b.split(".")
>>> c
['www', 'itdiffer', 'com']
>>> "*".join(c) #拼接字符串
'www*itdiffer*com'
>>> s1 = "I like {}".format("python") #格式化方法,其中{}作为占位符
>>> s1
'I like python'
>>> a
['good', 'python', 'I']
>>> a.append("like") #向list中增加str类型
>>> a
['good', 'python', 'I', 'like']
>>> new = [1,2,3]
>>> lst = ['python', 'qiwsir']
>>> lst.extend(new) #将另外的元素追加到已知列表中 append是整建制地追加,extend是个体化扩编。
>>> lst
['python', 'qiwsir', 1, 2, 3]
>>> all_users
['python', 'qiwsir', 'github', 'io']
>>> all_users.remove("io") #删除列表元素
>>> all_users
['python', 'qiwsir', 'github']
>>> all_users.pop(1) #删除列表所选编号元素
'qiwsir'
>>> all_users
['python', 'github']
>>> a = [3,5,1,6]
>>> a.reverse() #把列表元素顺序反过来
>>> a
[6, 1, 5, 3]
>>> a
[6, 1, 5, 3]
>>> a.sort() #对列表元素进行排序
>>> a
[1, 3, 5, 6]
>>> a.sort(reverse=True) #实现列表元素从大到小的排序
>>> a
[6, 5, 3, 1]
>>> lst = ["python", "java", "c", "pascal", "basic"]
>>> lst.sort(key=len) #以字符串的长度为关键词进行排序的
>>> lst
['c', 'java', 'basic', 'python', 'pascal']