2022-02-17 Python学习笔记
2022-02-17 22:51:30
由于中文注释会导致报错,所以文件开头要加一行注释
#coding:utf-8
file = open(‘文件路径路径’,w)#w表示没有就创建文件,有就打开
查看变量类型用print(type(变量))
字符串用法(字符串即引号中的内容)
word = ' a loooooong word'
num = 12
string = 'bang!'
total = string*(len(word)-num) #等价于字符串‘bang!'*4 len表示长度
print(total)
字符串的分片与索引 [ ]
按顺序 从左往右是0~∞ 从右往左是-1~-∞
字符串的方法
phone_number = '1386-666-0006'
hiding_number = phone_number.replace(phone_number[:9],'*'*9) #replace()替换
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',vern = 'came')) #preposition 介词 verb 动词
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) #填充网址中空缺的城市数据
print()是一个放入对象就能将结果打印的函数
input()是一个可以让用户输入信息的函数
len()是一个可以测量对象长度的函数
int()是一个可以讲字符串类型的数字转换成整数类型的函数

浙公网安备 33010602011771号