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))
    #每个字符后面都插入指定的字符,构成一个新的字符串
  • 相关阅读:
    Android 开发学习进程0.19 webview 的使用
    2020年4到6月—7家公司面试总结(3家已拿offer)
    iOS今日头条第3轮面试回忆
    [搬运]Dart之枚举中使用扩展
    Proguard 常用规则
    shiro安全框架
    Android服务的AIDL跨进程(程序)操作
    Android——服务的实例,银行服务
    Android四大组件之服务————服务的生命周期和启动方式
    Android 程序间的广播和Manifest找不到(解决方法)
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9286508.html
Copyright © 2011-2022 走看看