常用命令:
赋值
a=12
输出
print(a)
写入
file=open(r'C:\Users\Mean\Desktop\test.txt','w')
file.write('HelloWorld')
file.close()
注意:单引号等价于双引号,三引号可换行。
连接字符串
num = 1
string ='1'
num2 = int(string) #转换类型
print(num + num2)
字符串分片索引
name ='My name is Mike'
print(name[0])
'M'
print(name[-4])
'M'
print(name[11:14]) # from 11th to 14th, 14th one is excluded
'Mik'
print(name[11:15]) # from 11th to 15th, 15th one is excluded
'Mike'
print(name[5:])
'me isMike'
print(name[:5])
'Myna'
字符串方法len
a=len('zhangsan')
字符串方法replace
phone_number= '1386-666-0006'
hiding_number= phone_number.replace(phone_number[:9],'*' * 9)
print(hiding_number)
*********0006
字符串方法format
print('{}a word she can get what she {} for.'.format('With','came'))
print('{preposition}a word she can get what she {verb} for'.format(preposition = 'With',verb ='came')) print('{0} a word she can get what she {1}for.'.format('With','came'))
city =input("write down the name of city:")
url= "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city)