python字符串和字符没有区别,一个字符看作为字符串长度为1的字符串。具体的增删查改如下:
#字符串
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
s = 'First line.\nSecond line.'
prefix1 = 'Py'
prefix2= 'thon'
prefix= prefix1+prefix2
print( prefix )
print( 3*prefix1 )
word = 'Python'
print( word[0] )
print( word[-1] )
print(word[0:2])
print(word[:2]+word[2:])
s=len(word)
print(s)
#列表
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[0] )
print( list[1] )
print( list[2] )
print( list[-3:] )
list2=list+[34,23,234,23]
print(list2)
#列表是允许修改元素的
#将list2[7]=23的值改为55
list2[7]=55
cubes = [1, 8, 27, 65, 125]
cubes.append(216)
cubes.append(7 ** 3)
print(cubes)
#对切片赋值或者清空它
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters[2:5] = ['C','D','E']
#清空
letters[2:5] = []
#计算长度
print(len(letters))
#允许嵌套列表
a = ['a','b','c']
n = ['1','2','3']
x = [a,n]
print(x)
print(x[0][1])
#生成一个斐波那契序列
a,b=0,1
while b<10:
print(b)
a,b=b,a+b
|