Python学习笔记——变量&字符串基本概念和常用方法

tips

  1. python 区分大小写
  2. 空格
  3. print() 函数sep作为可选参数,意为以…分开,默认以’ ’ 空格分开
# 圣诞树
print('  *',' * *','* * *','  |   ',sep='\n') #print() 函数sep作为可选参数,意为以...分开,默认以' ' 空格分开
  1. type() 获取变量类型
  2. len() 获取对象长度
  3. 引号,字符串使用成对的单引号’ ’ 、双引号" "、三引号 ‘’’ ‘’’ ,三引号用于过长的字符串,字符串内允许换行;
  4. 过长的代码可以用\来换行,同一行的代码会有缩进,ide可直接换行
  5. 类型切换:类型名(变量):eg int(‘123’)
  6. 字符串相乘,字符串*n,重复打印出N倍的字符串,如下
  str1='words' 
  print(str1*3) 
  wordswordswords 
  1. 由于中文注释会导致报错,所以需要在文件开头加如下注释
   #coding:utf-8 
  1. 字符串的replace(需要替换的字符串,替换成的字符串) 方法
>>> phonenumber='13888888888'
>>> hidenumber=phonenumber.replace(phonenumber[:7],'*'*7)
>>> print(hidenumber)
*******8888
  1. 字符串的find(需要查找的字符串)方法,返回查找到的第一个位置的第一个字符的索引
>>> phonenumber='13888888888'
>>> phonenumber.find('388')
1
  1. 字符串的格式化啊函数format()
>>> print('{} a word she can get what she {} for .'.format('With','came'))
With a word she can get what she came for .
>>> city=input('write down the name of city:')
write down the name of city:HangZhou
>>> url='http://apistore.baidu.com/microservice/weather?citypinyin={}'.format(city)
>>> print (url)
http://apistore.baidu.com/microservice/weather?citypinyin=HangZhou
>>>   
  1. python下标从0开始eg:[startindex:endindex] 标识从索引startindex开始截取,一直到endindex-1 的索引字符串;startindex和endindex可以为空,为空分别表示从0开始和到最后一个索引结束

    在这里插入图片描述
    在这里插入图片描述

  2. 文件操作,如果路径下没有文件,自动create

file =open('filepath/filename','w')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值