# 赋值是单等号 =,相等判断是双等号 == # 还有一个相等判断保留字 is # is 判断前后两者是否指向同一个对象(如果是两个对象,就算值相同,也会返回False) # == 只判断值是否相同 a = [1, 2, 3, 4] # Point a at a new list, [1, 2, 3, 4] b = a # Point b at what a is pointing to b is a # => True, a and b refer to the same object b == a # => True, a's and b's objects are equal b = [1, 2, 3, 4] # Point b at a new list, [1, 2, 3, 4] b is a # => False, a and b do not refer to the same object b == a # => True, a's and b's objects are equal
1 2 3
# 通过‘或者“可以创建string "This is a string." 'This is also a string.'
# 一个string可以看作是一个char的list "This is a string"[0] # => 'T'
'T'
1 2
# len()是一个保留函数,可以计算list的长度 len("This is a string") # => 16
16
1 2
# python中的string对象,有.format方法,可以用来对该string进行格式化操作 "{} can be {}".format("Strings", "interpolated") # => "Strings can be interpolated"
'Strings can be interpolated'
1 2
# 可以通过在大括号{}中添加format参数的index来进行填充指定 "{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
'Jack be nimble, Jack be quick, Jack jump over the candle stick'
1 2
# 也可以给format中的参数命名,来代替index. "{name} wants to eat {food}".format(name="Bob", food="lasagna")
'Bob wants to eat lasagna'
1 2
# 如果需要兼容python2,老版的format写法如下 "%s can be %s the %s way" % ("Strings", "interpolated", "old")
'Strings can be interpolated the old way'
1 2 3 4 5
# 在python3.6之后的版本中,可以在string前加f来进行format操作 name = "Reiko" f"She said her name is {name}."# => "She said her name is Reiko" # 在大括号中,也可以调用python的方法 f"{name} is {len(name)} characters long."