• Python-字符串常用函数


    可以在shell里面输入help(str)可以看出有哪些函数

    字符串方法:

    • len(s)  测字符串长度
    • int(s)   将字符串转换成整形
    • ord(s)与chr(ASCII)  将字符串和ASC码转换
    •  S.find(sub [,start [,end]]) -> int   
      • rfind  从右往左开始找
    • strip(s)去除首尾空格
      • lstrip  去除左边的空格
      • rstrip  去除右边的空格
    • splita(s)   分割
    # -*- coding: cp936 -*-
    #****************字符串常用函数**********
    #******1. len(s) 字符串长度**************
    s = "www.baidu.com"
    print len(s)
    li = [1] * 6
    print len(li)
    #2. int(s) 将字符串转换为整形************
    s1 = "123"
    print int(s1)
    s3 = "a123b"
    #***error***a = int(s3)
    a = int(s3[1:len(s3) - 1])
    b = int(s3[1:-1])
    print a  #为int,不能 +  "a =" 
    print b
    #3.ord(s)与chr(ASCII)  将字符串和ASC码转换*************
    print ord('a')
    print chr(99)
    #4.S.find(sub [,start [,end]]) -> int*************
    sub = "baidu"
    s = "www.baidu.com"*5
    print s.find(sub)  #从头开始
    print s[4:4 + len(sub)]
    print s.find(sub,5)  #从第5个位置开始找
    print s.rfind(sub)   #rfind  从右往左开始找
    #5.strip(s)去除首尾空格**************
    sp = "    www.baidu.com    ** " #字符和*号中间的空格没有去除
    print sp
    s = sp.strip()
    print s
    sl = sp.lstrip()  #sr = sp.rstrip()
    print sl
    View Code

    # -*- coding: cp936 -*-
    #--------------字符串的分割  split----------------
    
    #字符串和list之间有很多不得不说的事
    #比如有同学想要用python去自动抓取某个网页上的下载链接,
    #那就需要对网页的代码进行处理。处理的过程中,免不了要在字符串和list之间进行很多操作。
    
    #我们先从最基本的开始。假设你现在拿到了一个英语句子,
    #需要把这个句子中的每一个单词拿出来单独处理。
    sentence = 'i am an English sentence'
    
    #对字符串进行分割
    x = sentence.split()
    #split()会把字符串按照其中的空格进行分割,
    #分割后的每一段都是一个新的字符串,最终返回这些字符串组成一个list
    print x
    #执行结果---返回的是个list列表
    #['i', 'am', 'an', 'English', 'sentence']
    #原来字符串中的空格不再存在。
    
    #除了空格外,split()同时也会按照换行符
    ,制表符	进行分割。
    #所以应该说,split默认是按照空白字符进行分割。
    #之所以说默认,是因为split还可以指定分割的符号。比如你有一个很长的字符串
    
    section = 'Hi. I am the one. Bye.'
    #通过指定分隔符号为 '.' ,可以把每句话分开
    y = section.split('.')
    print y
    #['Hi', ' I am the one', ' Bye', '']
    #这时候,'.'作为分割符被去掉了,而空格仍然保留在它的位置上。
    #注意最后那个空字符串。每个'.'都会被作为分割符,
    #即使它的后面没有其他字符,也会有一个空串被分割出来。
    
    print 'aaa'.split('a')
    #执行结果----['', '', '', '']
    View Code



    不太常用的函数 

    • isalnum 判断字符串是否是合法字符构成的(数字和字母)
    • isalpha   判断是否都是字符
    • isdigit    判断是否都是数字

    密码一般都要求为 字母加数字,这样可以判断密码是否过于简单

    • islow()  判断字符串是否小写
    • isupper() 判断大写
    • isspace()  判断空字符串
    • lower()  变成小写
    • upper()  变成大写

    字符串的查找和替换函数

    • startswith
    • endswith
    • find


    字符串修改:

    • 值拷贝(字符串是不可修改的,只会创建新的字符串)
      • s = "baidu"
      • t = s[:3] + "x" + s[5:]
    • replace函数
      • "Mokaffe".replace("Mo","hello")  (旧的,新的)


    index(sub)

    rindex(sub)

    s.encode([encoding])

    s.decode([encoding])

  • 相关阅读:
    find 命令
    shell 脚本 测试webApp
    thinkphp模板继承
    Tp-validate进阶thinkphp
    B2B、B2C、C2C、O2O
    phpstorm自定义代码片段
    phpstorm开启xdebug断点调试,断点调试不成功来这里
    app接口设计
    socket
    字符集转换
  • 原文地址:https://www.cnblogs.com/Mokaffe/p/4421530.html
Copyright © 2020-2023  润新知