zoukankan      html  css  js  c++  java
  • Python 字符串操作函数二

    #-*- coding:utf-8 -*-
    
    line = "l want watch movie with you ."
    
    print(line.center(50))
    print(line.ljust(50))
    print(line.rjust(50))
    
    #center 字符串居中
    #ljust 字符串居左
    #rjust 字符中居右
    
    #lstrip 删除字符串左边的空白字符
    #rstrip 删除字符串右边的空白字符
    #strip 删除字符串两端的空白字符
    
    word = "
     	  i am a bird . 
     
     "
    
    print("==lstrip==")
    print(word.lstrip())
    
    
    print("===rstrip==")
    print(word.rstrip())
    
    
    print("==strip==")
    print(word.strip())
    
    #空白字符包括空格 /n /t等不可见字符
    
    
    nword = "hello this a new world ."
    print("==partition==")
    #partition
    print(nword.partition("a"))
    #在源字符串中从左向右查找指定字符串,找到指定字符串做切割,分为3部分,指定字符串前,后,其本身,返回值是元组
    #如果指定字符串不存在于源字符串中,那么结果由源字符串本身,两个空字符串组成
    
    #rpartition 从右向左查找指定字符串
    
    
    wline = "hello 
     world"
    print("==splitlines==")
    print(wline.splitlines())
    #splitlines 按照行进行分割,返回一个包含各行作为元素 的列表
    
    
    print("==isalpha==")
    print(wline.isalpha())  #返回false 因为存在空格,转义字符
    #isalpha 判断字符串中所有字符是否都是字母,字母返回true,其他返回false
    
    
    num = "12233"
    print("==isdigit==")
    print(num.isdigit())
    #isdigit 判断字符串中所有字符是否都是数字,是返回true,其他返回false
    
    
    str="sadfa213"
    print("==isalnum==")
    print(str.isalnum())
    #isalnum 字符串是否只由数字和字母组成,是返回true,其他返回false
    
    
    space="  "
    print("==isspace==")
    print(space.isspace())
    #isspace 判断字符串中是否只有空格,是返回true,其他返回false
    
    
    print("==join==")
    tstr = "_"
    list1 = ["aa","bb"]
    print(tstr.join(list1))
    #每个字符后面都插入指定的字符,构成一个新的字符串
  • 相关阅读:
    Django U2 模型
    Django U0 使用Django
    Django H2 文档查看
    python模块--time模块/os模块/sys模块
    python模块-logging和collections以及random模块
    python-hashlib模块configparser模块logging模块
    python模块--序列化
    python面向对象的特殊方法和单例模式
    python类属性和类方法以及静态方法和反射
    python面向对象的三个特性
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9286508.html
Copyright © 2011-2022 走看看