#--------------------------------------------------------------------------------------- # 字符串连接 : 方法1 ‘+‘ str0 = ‘hello‘ str1 = "python" print(str0 + str1) #‘hellopython‘ # 字符串连接 : 方法2 连接对象.join() : 在每个单词之间用连接对象连接起来 lst = [‘www‘,‘google‘,‘com‘] lst_join = ".".join(lst) print(lst_join) #‘www.google.com‘ #--------------------------------------------------------------------------------------- # 格式化字符串1: 占位符‘{}‘ name = ‘Tom‘ age = 20 addr = "上海市" info = ‘{0},{1},{2}‘.format(name, age, addr) print(info) #‘Tom,20,上海市‘ #--------------------------------------------------------------------------------------- # 格式化字符串2: 占位符‘{}‘ print("{name},{age},{addr}".format(name = "Tom", age = 22, addr = "上海市")) #‘Tom, 22, 上海市‘ #--------------------------------------------------------------------------------------- # 格式化字符串3: % num = 100 str_tmp = "%d years" % num print(str_tmp) #‘100 years‘ lang = "python" print("I love %(program)s" % {"program" : lang}) #"I love python" #--------------------------------------------------------------------------------------- # len() : 获取字符串长度 str1 = "hello" print(len(str1)) #5 #--------------------------------------------------------------------------------------- # 字符串.isalpha() : 判断字符串对象是否全部为字母, True(全部为字符), False(含非字母) str2 = "world2" str3 = ‘hehe he‘ print(‘str1 : ‘ + str(str1.isalpha())) #True print(‘str2 : ‘ + str(str2.isalpha())) #False print(‘str3 : ‘ + str(str3.isalpha())) #False #-------------------------------------------------- # str.split() : 分割字符串 str4 = "www.itdiffer.com" for str_tmp in str4.split("."): print(str_tmp) #‘www‘ ‘itdiffer‘ ‘com‘ #-------------------------------------------------- # str.strip() : 去掉str左右两边的空格 str5 = " he ha ho " print(‘去掉前,str5:‘ + str5 + ‘:长度‘ + str(len(str5))) str5 = str5.strip(); print(str5) print(‘去掉后,str5:‘ + str5 + ‘:长度‘ + str(len(str5))) #-------------------------------------------------- # str.lstrip() : 去掉str左边的空格 str5 = " hehe" print(str5.lstrip()) #-------------------------------------------------- # str.rstrip() : 去掉str右边的空格 str6 = "hehe " print(str6.rstrip()) #-------------------------------------------------- # str.upper() : 将小写字母,全部转换为大写 str7 = "hello python" print(str7.upper()) #-------------------------------------------------- # str.lower() : 将大写全部转换为小写 str8 = "HELLO PYTHON" print(str8.lower()) #-------------------------------------------------- # str.isupper() : 判断当前字符串是否全部为大写 str9 = "hello" str10 = "HELLO" print(str(str9.isupper())) #False print(str(str10.isupper())) #True #-------------------------------------------------- # str.islower() : 判断当前字符串是否全部为小写 str11 = "Hello" str12 = "python" print(str(str11.islower())) #False print(str(str12.islower())) #True #-------------------------------------------------- # str.title() : 将字符串中每个单词的首字母转换为大写(如果字符串全部为大写,则转化结果为:每个单词的首字母大写其他字母小写) str13 = "hello Pyhton" str14 = "hello python" str15 = "HELLO PYTHON" str13_title = str13.title() #‘Hello Python‘ str14_title = str14.title() #‘Hello Python‘ str15_title = str15.title() #‘Hello Python‘ print(‘str13:{0}‘.format(str13)) print(‘str14:{0}‘.format(str14)) print(‘str15:{0}‘.format(str15)) print(‘str13_title:‘ + str13_title) #‘str13_title:Hello Pyhton‘ print(‘str14_title:‘ + str14_title) #‘str14_title:Hello Pyhton‘ print(‘str15_title:‘ + str15_title) #‘str15_title:Hello Pyhton‘ print(str(str13_title.istitle())) #True print(str(str14_title.istitle())) #True print(str(str15_title.istitle())) #True #-------------------------------------------------- # str.istitle() : 判断每个单词的首字母是否为大写 str16 = "hEllo" str17 = "World" str18 = "Hello,World" str19 = "Hello,world" print(str(str16.istitle())) #False print(str(str17.istitle())) #True print(str(str18.istitle())) #True print(str(str19.istitle())) #False #-------------------------------------------------- # str.capitalize() : 将字符串的第一个字母变为大写 str20 = "hello python" str20_capitalize = str20.capitalize(); print(str(str20_capitalize)) #‘Hello pythn‘
原文:http://www.cnblogs.com/DuanLaoYe/p/6671048.html