python基础1

#coding:utf-8 涉及中文编码问题 --支持中文注释
#!/usr/bin/python 告诉编译器去系统寻找python的解释器
 
my_first_py = 'Hello World'
print(my_first_py)
print(type(my_first_py))
 
#合并字符串
what_he_does = 'plays '
his_instrument = "guitar "
his_name = "Robert Johnson "
age = 10
name_and_age = 'heisenburg is %d old'%(age)
print(his_name +what_he_does +his_instrument,name_and_age)
print('I said:%s'%name_and_age)

formatter = "%r %r %r %r"
print(formatter %(1,2,3,4))

#int 和str转换
num = 1
str = '1'
num2 = int(str)
print(num +num2)

#分片字符串
words = 'words'*3
name = 'my name is caoyajun'
print(words)
print(name[0],name[-4],name[11:14],name[11:15],name[11:],name[:11])#-4是从右往左第四个

#替换字符串
phone_number = '15013822382'
hide_number = phone_number.replace(phone_number[:9],'*'*9)
print(phone_number,hide_number)

#合并字符串不同使用方式
print('{} a word she can  get  what  she {} from'.format('with','come'))#python2.6.x不支持
print('{0} a word  she  can  get what she {1} from'.format('with','come'))
print('{first} a word she can  get  what she {second} from'.format(first = 'with',second = 'come'))

#raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,
# 即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError
city = input("write down the name of city:")#控制台输入'beijing',按python语法
url = "http://apistore.baidu.com/microservice/weather?citypinyin={first}".format(first =city)
print (url)


#函数 :后面相当于一个block代码块
def fahrenheit_converter(C):
    fahrenheit = C*9/5 +32
    #str_f = str(fahrenheit) + 'F'
    return fahrenheit
print(fahrenheit_converter(32))

#print('  *',' **','***',' |',sep = '\n')报错
print('  *',' **','***',' |')

#文件
file = open('/Users/cykj/DeskTop/text.txt','w')#以写的方式打开,不存在文件就自动创建一个文件print(file)#打印的是文件相关信息success = file.write('This is my fisrt python file')print(success)#打印29刚好是字符串长度file.write('change the file')//#add追加内容state = file.closefil = open('/Users/cykj/DeskTop/text.txt','w')fil.write('hhhhhhhhhhhhhh')#覆盖原来的内容#文件存在写入,不存在创建文件def create_txt_file(name,msg): path = '/Users/cykj/DeskTop/' full_path = path + name + '.txt' file = open(full_path,'w') file.write(msg) file.close print('Done')create_txt_file(name='caoyajun',msg='this is my own file')#过滤敏感信息def text_filter(word,source_word ='source',replace_word ='replace'): return word.replace(source_word,replace_word)print(text_filter('source source source'))#replace replace replace#过滤信息然后存储过滤好的信息def filer_msg_and_save(name,msg): clean_msg =text_filter(msg) create_txt_file(name,clean_msg)filer_msg_and_save('caoyajun','source source kkkkkk source')#覆盖了原来的信息
文件text.txt:


 
文件caoyajun.txt:
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值