Python中encode方法的使用简介
“想吃怕胖”通过精心收集,向本站投稿了10篇Python中encode方法的使用简介,下面是小编给大家带来关于Python中encode方法的使用简介,一起来看看吧,希望对您有所帮助。
篇1:Python中encode方法的使用简介
这篇文章主要介绍了Python中encode方法的使用简介,是Python入门中的基础知识,需要的朋友可以参考下
encode() 方法返回字符串的编码版本,默认编码是当前的默认字符串编码。可给予设置不同的错误处理机制。
语法
以下是encode()方法的语法:
str.encode(encoding=‘UTF-8‘,errors=‘strict‘)
参数
encoding -- 这是所使用的编码。对于所有的编码方案的列表,请访问:标准编码库
errors -- 这可能是给定一个不同的错误处理机制。默认的错误是“严格”,即编码错误提出UnicodeError,
其他可能的值是ignore‘, ‘replace‘, ‘xmlcharrefreplace‘, ‘backslashreplace‘ 并通过codecs.register_error().注册的任何其他名称。
返回值
此方法返回的字符串的编码版本。
例子
下面的例子显示了encode()方法的使用。
#!/usr/bin/pythonstr = “this is string example....wow!!!”;print “Encoded String: ” + str.encode(‘base64‘,‘strict‘)
当我们运行上面的程序,它会产生以下结果:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
篇2:Python中的hypot方法使用简介
这篇文章主要介绍了Python中的hypot()方法使用简介,是Python入门所需掌握的基础知识,需要的朋友可以参考下
hypot()方法返回的欧几里德范数 sqrt(x*x + y*y).
语法
以下是hypot()方法的语法:
hypot(x, y)
注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数
参数
x -- 这必须是一个数值
y -- 此方法返回欧几里德范数 sqrt(x*x + y*y)
返回值
此方法返回欧几里德范数 sqrt(x*x + y*y)
例子
下面的例子显示 hypot()方法的使用,
Python中的hypot()方法使用简介
,
#!/usr/bin/pythonimport mathprint “hypot(3, 2) : ”, math.hypot(3, 2)print “hypot(-3, 3) : ”, math.hypot(-3, 3)print “hypot(0, 2) : ”, math.hypot(0, 2)
当我们运行上面的程序,它会产生以下结果:
hypot(3, 2) : 3.60555127546hypot(-3, 3) : 4.24264068712hypot(0, 2) : 2.0
篇3:浅谈Python中copy方法的使用
这篇文章主要介绍了浅谈Python中copy方法的使用,Python中的拷贝分为潜拷贝和深拷贝,本文只是简单介绍用法,需要的朋友可以参考下
copy()方法返回字典的浅拷贝,
语法
以下是copy()方法的语法:
dict.copy()
参数
NA
返回值
此方法返回字典的浅拷贝,
例子
下面的例子显示了copy()方法的使用。
#!/usr/bin/pythondict1 = {‘Name‘: ‘Zara‘, ‘Age‘: 7};dict2 = dict1.copy()print “New Dictinary : %s” % str(dict2)
当我们运行上面的程序,它会产生以下结果:
New Dictinary : {‘Age‘: 7, ‘Name‘: ‘Zara‘}
篇4:Python中的匿名函数使用简介
作者:廖雪峰 字体:[增加 减小] 类型:
这篇文章主要介绍了Python中的匿名函数的使用,lambda是各个现代编程语言中的重要功能,需要的朋友可以参考下
当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便,
在Python中,对匿名函数提供了有限支持。还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x)的函数外,还可以直接传入匿名函数:
>>> map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])[1, 4, 9, 16, 25, 36, 49, 64, 81]
通过对比可以看出,匿名函数lambda x: x * x实际上就是:
def f(x): return x * x
关键字lambda表示匿名函数,冒号前面的x表示函数参数,
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。此外,匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数:
>>> f = lambda x: x * x>>> f 同样,也可以把匿名函数作为返回值返回,比如: def build(x, y): return lambda: x * x + y * y 小结 Python对匿名函数的支持有限,只有一些简单的情况下可以使用匿名函数。 这篇文章主要介绍了Python中处理字符串之endswith方法的使用,是Python入门中的基础知识,需要的朋友可以参考下 endswith()方法返回true,如果字符串以指定后缀结尾,否则返回(False可选限制的匹配从给定的索引开始和结束), 语法 以下是endswith()方法的语法: str.endswith(suffix[, start[, end]]) 参数 suffix -- 这可能是一个字符串或者是元组用于查找后缀。 start -- 切片从此开始 end -- 切片到此为止 返回值 如果字符串以指定的后缀结束此方法返回true,否则返回false, 例子 下面的例子显示了endswith()方法的使用。 #!/usr/bin/pythonstr = “this is string example....wow!!!”;suffix = “wow!!!”;print str.endswith(suffix);print str.endswith(suffix,20);suffix = “is”;print str.endswith(suffix, 2, 4);print str.endswith(suffix, 2, 6); 当我们运行上面的程序,它会产生以下结果: TrueTrueTrueFalse
这篇文章主要介绍了探究Python中isalnum()方法的使用,是Python入门学习中的基础知识,需要的朋友可以参考下 isalnum()方法检查判断字符串是否包含字母数字字符, 语法 以下是isalnum()方法的语法: str.isa1num() 参数 NA 返回值 如果字符串中的所有字符字母数字和至少有一个字符此方法返回 true,否则返回false, 例子 下面的例子显示了isalnum()方法的使用。 #!/usr/bin/pythonstr = “this”; # No space in this stringprint str.isalnum();str = “this is string example....wow!!!”;print str.isalnum(); 当我们运行上面的程序,它会产生以下结果: TrueFalse
这篇文章主要介绍了详解Python中find方法的使用,是Python学习中的基础知识,需要的朋友可以参考下 find()方法判断字符串str,如果起始索引beg和结束end索引能找到在字符串或字符串的一个子串中, 语法 以下是find()方法的语法: str.find(str, beg=0 end=len(string)) 参数 str -- 此选项指定要搜索的字符串。 beg -- 这是开始索引,默认情况下为 0, end -- 这是结束索引,默认情况下它等于字符串的长度。 返回值 如果找到此方法返回的索引,否则返回-1。 例子 下面的例子显示了find()方法的使用。 #!/usr/bin/pythonstr1 = “this is string example....wow!!!”;str2 = “exam”;print str1.find(str2);print str1.find(str2, 10);print str1.find(str2, 40); 当我们运行上面的程序,它会产生以下结果: 1515-1
这篇文章主要介绍了详解Python中expandtabs()方法的使用,是Python入门中的基础知识,需要的朋友可以参考下 expandtabs()方法返回制表符,即该字符串的一个副本, ‘t‘已经使用的空间,可选择使用给定的tabsize(默认8)扩展。 语法 以下是expandtabs()方法的语法: str.expandtabs(tabsize=8) 参数 tabsize -- 此选项指定要替换为制表符“t‘ 的字符数. 返回值 此方法返回在制表符,即通过空格进行了扩展字符串,‘t‘的副本, 例子 下面的例子显示expandtabs()方法的使用。 #!/usr/bin/pythonstr = “this iststring example....wow!!!”;print “Original string: ” + str;print “Defualt exapanded tab: ” + str.expandtabs();print “Double exapanded tab: ” + str.expandtabs(16); 当我们运行上面的程序,它会产生以下结果: Original string: this is string example....wow!!!Defualt exapanded tab: this is string example....wow!!!Double exapanded tab: this is string example....wow!!!
这篇文章主要介绍了Python中tell方法的使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下 tell()方法返回的文件内的文件读/写指针的当前位置, 语法 以下是tell()方法的语法: fileObject.tell() 参数 NA 返回值 此方法返回该文件中读出的文件/写指针的当前位置。 例子 下面的例子显示了tell()方法的使用, #!/usr/bin/python# Open a filefo = open(“foo.txt”, “rw+”)print “Name of the file: ”, fo.name# Assuming file has following 5 lines# This is 1st line# This is 2nd line# This is 3rd line# This is 4th line# This is 5th lineline = fo.readline()print “Read Line: %s” % (line)# Get the current position of the file.pos = fo.tell()print “Current Position: %d” % (pos)# Close opend filefo.close() 当我们运行上面的程序,它会产生以下结果: Name of the file: foo.txtRead Line: This is 1st lineCurrent Position: 18
这篇文章主要介绍了Python中degrees()方法的使用,是Python入门中的基础知识,需要的朋友可以参考下 degrees()方法从弧度转换到度角x 语法 以下是degrees()方法的语法: degrees(x) 注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数, 参数 返回值 x -- 这必须是一个数值。 此方法返回一个角度的度数值。 例子 下面的例子显示degrees()方法的使用, #!/usr/bin/pythonimport mathprint “degrees(3) : ”, math.degrees(3)print “degrees(-3) : ”, math.degrees(-3)print “degrees(0) : ”, math.degrees(0)print “degrees(math.pi) : ”, math.degrees(math.pi)print “degrees(math.pi/2) : ”, math.degrees(math.pi/2)print “degrees(math.pi/4) : ”, math.degrees(math.pi/4) 当我们运行上面的程序,它会产生以下结果: degrees(3) : 171.887338539degrees(-3) : -171.887338539degrees(0) : 0.0degrees(math.pi) : 180.0degrees(math.pi/2) : 90.0degrees(math.pi/4) : 45.0
【Python中encode方法的使用简介】相关文章: 2.python使用cStringIO实现临时内存文件访问的方法 7.记音方法简介篇5:Python中处理字符串之endswith方法的使用简介
篇6:探究Python中isalnum方法的使用
篇7:详解Python中find方法的使用
篇8:详解Python中expandtabs方法的使用
篇9:Python中tell方法的使用详解
篇10:解读Python中degrees方法的使用






文档为doc格式