编程小白的第一本 python 入门书 学习笔记02 第三章 变量与字符串



常用命令:

赋值

a=12

 

输出

printa

 

写入

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值