3.python基础知识

  1. 编码:
    8位=1byte 256
    16位=2byte 65536
    24位=3byte 16777216
    GB2312–>GBK
  2. 转化大小写
print(name1.upper())   #  转换为大写
print(name1.lower())    #转换为小写 
  1. 索引字符串的位置
#索引为第一个字符串的位置 ,从第4个开始到第11   打印出来的是整型
print(name1.index("B根自从c进城",4,12))    
  1. 编码
#编码 默认编码格式是UTF-8 , encoding="可以选择编码格式"
bs = name1.encode(encoding="utf-8")
  1. 解码
print(bs.decode(encoding="gbk"))
  1. 加入间隔符
name1 = "刘老根和药匣子.jpg"
print(",".join(name1))
#刘,老,根,和,药,匣,子,.,j,p,g
  1. 把字符串分成列表
print(name2.split("-"))   #列表
#如果没有括号内的标识符,默认分为一个列表
  1. 把指定的字符串 同其他的字符串替换 替换次数
print(name1.replace("刘老根","大辣椒",1))
  1. 查询出现的次数
print(name1.count("刘老根",1))
  1. 去除字符串左右的 特定字符串 默认是空格
name1 = "ccccc刘老根和药匣子.jpgccccc"
print(name1.strip('c'))
#刘老根和药匣子.jpg
  1. 右对齐去除 默认空格
name1 = "ccccc刘老根和药匣子.jpgccccc"
print(name1.lstrip("c"))
#刘老根和药匣子.jpgccccc
  1. 左对齐去除 默认空格
print(name1.rstrip())
  1. format格式化
 print('{} {}'.format('hello','world'))  # 不带字段
 2 hello world
 3 >>> print('{0} {1}'.format('hello','world'))  # 带数字编号
 4 hello world
 5 >>> print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
 6 hello world hello
 7 >>> print('{1} {1} {0}'.format('hello','world'))
 8 world world hello
  >>> print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字
10 world hello world
  1. find查找 如果在字符串里找不到返回-1
# print(name1.find("国"))         #返回-1
# print(name1.index("国"))         #找不到报错
  1. 判断开头和结果字符串是否正确 返回布尔类型
print(name1.endswith(".jpg"))
print(name1.startswith("刘老根"))

16.进行字符串的补齐

name1 = "刘老根和药匣子"
print(name1.rjust(15,"*"))     #以右对齐 一共要15个字节 缺少的用*在左侧补全
print(name1.ljust(10,"+"))     #以左对齐 一共要10个字节 缺少的用+在右侧补齐 
  1. 导入tushare
    pip install tushare
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值