• Python字符串操作


    1. isalnum()判断是否都是有效字符串
      >>> ev1 = 'evilxr'
      >>> ev2 = 'ev1il2xr3'
      >>> ev3 = '.,/!@#'
      >>> a = ev1.isalnum()
      >>> print a
      True
      >>> b = ev2.isalnum()
      >>> print b 
      True
      >>> c = ev3.isalnum()
      >>> print c
      False
      
    2. isalpha和isdigit可分别判断字符串里是否都是由字符或者数字组成
      >>> ev1.isalpha()
      True
      >>> ev2.isalpha()
      False
      >>> ev2.isdigit()
      False
      >>> ev3.isdigit()
      False
      >>> ev2.isalpha()
      False
      >>> ev4.isdigit()
      True
      >>> ev4.isalpha()
      False
      >>> 
      

      可以用来检测密码的强度~

      >>> xr = raw_input('Please input your password:')
      Please input your password:evilxr1234
      >>> xr.isalpha()
      False
      >>> xr.isdigit()
      False
      >>> xr.isalnum()
      True
      >>> 
      
    3. 判断字符的大小写
      >>> xr = raw_input('Please input your password:')
      Please input your password:EVILXR
      >>> xr.islower()    #xr的内容是小写的吗?
      False
      >>> xr.isupper()      #xr的内容是大写的吗?
      True
      >>> 
      
    4. 判断是否全由空格组成
      >>> xr1 = '          '
      >>> xr1.isspace()
      True
      >>> xr2 = '  evilxr  '
      >>> xr2.isspace()
      False
      >>> 
      
    5. 字符的大小写转换
      >>> 'evilxr'.upper()    #将小写字符全转换为大写
      'EVILXR'
      >>> 'HEY,WELCOME TO MY BLOG!'.lower()    #将大写字符全转换为小写
      'hey,welcome to my blog!'
      >>> 'Hey,My name is Evilxr!'.upper()    #大小写混合的也能转
      'HEY,MY NAME IS EVILXR!'
      >>> 'Hey,My name is Evilxr!'.lower()
      'hey,my name is evilxr!'
      >>> 
      
    6. 去掉字符串左面或者右面的空格
      [root@localhost test]# cat 1.py
      ev1 = ' ev il xr '
      print ev1
      ev2 = ev1.lstrip()    #去掉左边
      print ev2
      ev3 = ev1.rstrip()    #去掉右边
      print ev3
      
      [root@localhost test]# python 1.py
       ev il xr 
      ev il xr 
       ev il xr
      
    7. 判断字符串的开始和结束
      [root@localhost test]# cat 2.py
      s1 = '.com'
      s2 = '.cn'
      s3 = 'www.'
      s4 = 'www.evilxr.com'
      
      if s4.startswith(s3):
      	print 'startswith www'
      else:
      	print 'start is not www'
      if s4.endswith(s1):
      	print 'endswith is com'
      elif s2.endswith(s2):
      	print 'endswith is cn'
      else:
      	print 'endswith is not com and cn'
      [root@localhost test]# python 2.py
      startswith www
      endswith is com
      [root@localhost test]# 
       
    8. replace()函数的使用,值拷贝
      >>> ev = 'www.evilxr.com'
      >>> id (ev)
      3078278264L
      >>> s1 = ev.replace('e','E')
      >>> print s1
      www.Evilxr.com
      >>> id(s1)
      3078278584L
      >>> ev1 = ev.replace('ev','EV')
      >>> print ev1
      www.EVilxr.com
      >>> id(ev1)
      3078278504L
      
      >>> ev2 = ev.replace('evilxr','evilxr.upper()')#upper()被当成了要被替换内容的一部分
      >>> print ev2
      www.evilxr.upper().com
      >>> ev3 = ev.replace('evilxr','evilxr'.upper())#正确的应该是这个
      >>> print ev3
      www.EVILXR.com
      >>> id(ev2)
      3078232832L
      >>> id(ev3)
      3078278704L
      >>> 
      
      #用切片看下
      >>> ev = 'evilxr'
      >>> ev1 = ev[:3]
      >>> print ev1
      evi
      >>> ev2 = ev[4:]
      >>> print ev2
      xr
      >>> ev3 = ev[:3]+'L'+ev[4:]
      >>> print ev3
      eviLxr
      
    9. 域名匹配
      re.match(r'^(?:[a-zA-Z0-9][a-zA-Z0-9-]*.)*([a-zA-Z0-9][a-zA-Z0-9-]*?.[a-zA-Z]{2,6}(?<!.cn)(?:.cn)?)$', 'a.b.124.ttt.com.cn').groups()
      
    10. python的json.dumps输出中文,指定ensure_ascii参数为False
      Signature: json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, sort_keys=False, **kw)
      
      --------------------------------------------------------------------------------------------------------
      In [11]: print json.dumps({'test':"西戎的博客园"},ensure_ascii=False)
      {"test": "西戎的博客园"}
      
      In [12]: print json.dumps({'test':"西戎的博客园"})
      {"test": "u897fu620eu7684u535au5ba2u56ed"}
      
    若非特别声明,文章均为Evilxr的个人笔记,转载请注明出处。
  • 相关阅读:
    attempted to return null from a method with a primitive return type (Double).
    window7 虚拟机安装
    DB 与oracle 批量新增的写法
    oracle 修改表
    备份还原oracle数据库
    oracle数据库的字符集更改
    IMP-00013
    oracle创建用户授权权限
    java中添加定时任务
    程序在运行过程中变量的保存位置与生命周期
  • 原文地址:https://www.cnblogs.com/evilxr/p/3769577.html
Copyright © 2020-2023  润新知