Python 字符串 center() 方法Python 字符串方法实例打印单词 "banana",占用 20 个字符,并使 "banana" 居中:txt = "banana"x = txt.center(20)print(x)完整实例:txt = "banana"x
Python 字符串 casefold() 方法Python 字符串方法实例将字符串设为小写:txt = "Hello, And Welcome To My World!"x = txt.casefold()print(x)完整实例:txt = "Hello, And Wel
Python 字符串 capitalize() 方法Python 字符串方法实例大写这句话的第一个字母:txt = "hello, and welcome to my world."x = txt.capitalize()print (x)完整实例:txt = "hel
Python zip() 函数Python 内建函数实例把两个元组连接起来:a = ("Bill", "Steve", "Elon")b = ("Gates", "Jobs", "Musk")x = zip(a, b)完整实例:a = ("Bill", "Steve", "Elon
Python vars() 函数Python 内建函数实例返回名为 Person 的对象的 __dict__ 属性:class Person: name = "Bill" age = 19 country = "USA"x = vars(Person)完整实例:class
Python type() 函数Python 内建函数实例返回这些对象的类型:a = ('apple', 'banana', 'cherry')b = "Hello World"c = 55x = type(a)y = type(b)z = type(c)完整实例:a = ('ap
Python tuple() 函数Python 内建函数实例创建包含水果名的元组:x = tuple(('apple', 'banana', 'cherry'))完整实例:x = tuple(("apple", "banana", "cherry"))print(x)定义和
Python super() 函数Python 内建函数实例创建一个将从另一个类继承所有方法和属性的类:class Parent: def __init__(self, txt): self.message def printmessage(self):
Python sum() 函数Python 内建函数实例将所有项添加到一个元组中,并返回结果:a = (1, 2, 3, 4, 5)x = sum(a)完整实例:a = (1, 2, 3, 4, 5)x = sum(a)print(x)定义和用法sum()
Python str() 函数Python 内建函数实例把数字 3.5 转换为字符串:x = str(3.14)完整实例:x = str(3.14)print(x)定义和用法str() 函数将指定的值转换为字符串。语法str(object,
Python sorted() 函数Python 内建函数实例对元组排序:a = ("b", "g", "a", "d", "f", "c", "h", "e")x = sorted(a)print(x)完整实例:a = ("b", "g", "a", "d", &qu
Python slice() 函数Python 内建函数实例创建一个元组和一个 slice 对象。使用 slice 对象仅获取元组的前两项:a = ("a", "b", "c", "d", "e", "f", "g", "h")x = slice(2)pr
Python setattr() 函数Python 内建函数实例改变 "person" 对象的 "age" 属性的值:class Person: name = "John" age = 36 country = "Norway"setattr(Person, 'age', 40)
Python set() 函数Python 内建函数实例创建包含水果名称的集合:x = set(('apple', 'banana', 'cherry'))完整实例:x = set(("apple", "banana", "cherry"))print(x)# Note: th
Python round() 函数Python 内建函数实例将数字四舍五入为仅两个小数:x = round(3.1415926, 2)print(x)完整实例:x = round(3.1415926, 2)print(x)定义和用法round() 函数返回