1 info = ‘abc’
info[2] = ‘d’
结果是什么,为什么会报错呢?
TypeError: ‘str’ object does not support item assignment
字符串不支持字符赋值。
2 如果要把上面的字符串info里面的c替换成d,要怎么操作呢?
info = 'abc'
b=info.replace('c','d')
print (b)
3 下面2个变量
a = ‘1’
b = 2
print a + b 的结果是什么,为什么会出现这个结果,如果希望结果是3,要怎么操作?
can only concatenate str (not "int") to str
a = 1
b = 2
print (a + b)
4 字符串:
a = ‘abcd’
用2个方法取出字母d
print (a[3])
print (a[3:4])
5 a = ‘jay’
b = ‘python’
用字符串拼接的方法输出:
my name is jay,i love python.
a = 'jay'
b = 'python'
print ("my name is %s,i love %s." %(a,b))
a = 'jay'
b = 'python'
c='my name is '
d=',i love '
print ("".join([c,a,d,b]))
6 a = ‘pyer’
b = ‘apple’
用字典和format方法实现:
my name is pyer, i love apple.
字典:
a = 'pyer'
b = 'apple'
c="my name is %(name)s,i love %(fruit)s." % {"name":a,"fruit":b}
print (c)
Format:
a = 'pyer'
b = 'apple'
c="my name is {name},i love {fruit}.".format(name=a,fruit=b)
print (c)
7 打开文件info.txt,并且写入500这个数字。
a=open("info.txt","w")
a.write("500")
a.close()
b=open("info.txt","r")
print (b.read())