概述内容概要:一、python2 or 3二、
字符串拼接三、字符串四、列表、元祖五、
字典六、
集合七、练习 一、python2 or python3目前大多使用python2.7,随着时间的推移,python3将会成为python爱好者的主流。python2和3区别:1.PRINT IS A FUNCTION1 Old: print "The answer is", 2*2 New: print("The answer is", 2*2)2 Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline3 Old: print # Prints a newline4 New: print() # You must call the function!5 Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr)6 Old: print (x, y) # prints repr((x, y))7 New: print((x, y)) # Not the same as print(x, y)!2.某些库名改变例如:2.x3.x_winregwinregcopy_regcopyregQueuequeueSockerServersockerserverreprreprlib3.ALL IS UNICODE NOW所有的字符编码变为unicode 二、字符串拼接方法一:使用“+”(不推荐)原因:python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间name="wd"msg="my name is "+nameprint(msg)输出:my name is wd方法二:使用格式化字符串%s:字符串%d:整数%f:浮点数1 name="wd"2 age=223 job="IT"4 msg="my name is %s age %d job %s"%(name,age,job)5 print(msg)6 输出:7 my name is wd age 22 job IT方法三:使用format进行格式化输出(变量名替换)1 name="wd"2 age=223 job="IT"4 msg='''my name is:{_name}5 age is: {_age}6 job is: {_job}'''.format(_name=name,_age=age,_job=job)7 print(msg)8 输出:9 my name is:wd10 age is: 2211 job is: IT或者:(位置替换)1 name="wd"2 age=223 job="IT"4 msg='''my name is:{0}5 age is: {1}6 job is: {2}'''.format(name,age,job)7 print(msg)8 输出:9 my name is:wd10 age is: 2211 job is: IT总结:对比以上三种方法,使用+方式进行拼接字符串会开辟较多的内存空间,效率低,推荐使用第二种和第三种方法。 三、字符串1.字符串常用 *** 作移除空白(strip)分割(split)长度(len)索引(index)切片字符串对方法如下:1 class str(basestring):2 """3 str(object='') -> string45 Return a nice string representation of the object.6 If the argument is a string, the return value is the same object.7 """8 def capitalize(self):9 """ 首字母变大写 """10 """11 S.capitalize() -> string1213 Return a copy of the string S with only its first character14 capitalized.15 """16 return ""1718 def center(self, width, fillchar=None):19 """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """20 """21 S.center(width[, fillchar]) -> string2223 Return S centered in a string of length width. Padding is24 done using the specified fill character (default is a space)25 """26 return ""2728 def count(self, sub, start=None, end=None):29 """ 子序列个数 """30 """31 S.count(sub[, start[, end]]) -> int3233 Return the number of non-overlapping occurrences of substring sub in34 string S[start:end]. Optional arguments start and end are interpreted35 as in slice notation.36 """37 return 03839 def decode(self, encoding=None, errors=None):40 """ 解码 """41 """42 S.decode([encoding[,errors]]) -> object4344 Decodes S using the codec registered for encoding. encoding defaults45 to the default encoding. errors may be given to set a different error46 handling scheme. Default is 'strict' meaning that encoding errors raise47 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'48 as well as any other name registered with codecs.register_error that is49 able to handle UnicodeDecodeErrors.50 """51 return object()5253 def encode(self, encoding=None, errors=None):54 """ 编码,针对unicode """55 """56 S.encode([encoding[,errors]]) -> object5758 Encodes S using the codec registered for encoding. encoding defaults59 to the default encoding. errors may be given to set a different error60 handling scheme. Default is 'strict' meaning that encoding errors raise61 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and62 'xmlcharrefreplace' as well as any other name registered with63 codecs.register_error that is able to handle UnicodeEncodeErrors.64 """65 return object()6667 def endswith(self, suffix, start=None, end=None):68 """ 是否以 xxx 结束 """69 """70 S.endswith(suffix[, start[, end]]) -> bool7172 Return True if S ends with the specified suffix, False otherwise.73 With optional start, test S beginning at that position.74 With optional end, stop comparing S at that position.75 suffix can also be a tuple of strings to try.76 """77 return False7879 def expandtabs(self, tabsize=None):80 "
<table border="0">
<tr>
<td><span >一、python2 or python3</td>
</tr></table>
old: ,2*2 New: (,2*2 old: x, old: New: () old: >>sys.stderr, New: (,file= old: (x,y) New: ((x,y))
<table border="0">
<tr>
<td><span >二、字符串拼接</td>
</tr></table>
name==+ wd
name= age=22 job= msg=% my name wd age 22 job IT
name= age=22 job= msg= .format(_name=name,_age=age,_job= my name age : 22 job : IT
name= age=22 job= msg= my name age : 22 job : IT
<table border="0">
<tr>
<td><span >三、字符串</td>
</tr></table>
string string center(self,wIDth,fillchar= string count(self,sub,start=None,end= int decode(self,enCoding=None,errors= object encode(self,errors=