zoukankan      html  css  js  c++  java
  • Python之字符串

    Python之字符串

    返回首页

      字符串的常用功能:移除空白、分割、长度、索引、切片、格式化、查找、替换、拼接

      拼接:join可以做字符串拼接

    s="hello"+"world"+"I"+"am"+"python"
    print(s)
    
    print(" ".join(["I","am","world!"]))   #join里放要拼接的内容,将内容放在列表里。

      移除空白:strip()方法

    #去处空白,用Strip(),也是脱去什么的意思
    name = "
    	  george   
    "
    print(name)
    print(name.strip())   #strip可以去掉左边和右边的空格,tab键	 和换行
    

      分割:split()方法,将字符串分割成列表。

    #分割 split()括号中,可以指定以什么分割
    name = "G,e,o,r,g,e"
    print(name.split())   #默认将字符串按空格,分割成列表类型
    print(name.split(","))  #将字符串按"," 分割成列表类型

      

    s="hello world".split("l",1)    #数字1为分割次数  # ["he","lo world"]
    print(s)

      

      长度:len()方法,可以判断一个字符串有多少个字符。

    #长度 len
    name = "George"
    print(len(name))

      

      切片:顾头不顾尾

    #切片 字符串切片用[],里面标注起始位置和终点位置,用“:”分开。
    #切片的原则:顾头不顾尾
    name = "George,link"
    print(name[2:5])  #正向从左往右切片
    print(name[2:])  #正向从左往右切片
    print(name[-3: ])  #反向从右往左切片
    print(name[0::2])  #跳着切片,第二个":"后的数字是步长,也就是间隔几个字符切一次

     

      格式化字符串:

      %s 占位符(字符串)     %d 数字(整型)     %f 小数 (浮点型)  

    name = input("name>>:")
    age = input("age>>:")
    job = input("job>>:")
    salary = input("salary>>:")
    
    info = """
    ----------- info of %s ------------
    NAME = %s
    AGE = %d
    JOB = %s
    Salary = %fRMB
    """% (name,name,int(age),job,int(salary))
    print(info)

      字符串操作大集合:

    name = "george 	Wang is Nice"
    name2 = "My name is {0}, i am {1} years old "
    name3 = "My name is {name}, i am {age} years old "
    print(name.capitalize())    #首字母大写
    print(name.casefold())      #大写变小写 if choice == "Y" or choice =="y"
    print(name.center(50,'-'))  #定义字符串长度,字符不够用 '-' 补充。
    print(name.count('e',2))    #统计字符串中某个字符的个数,可以设置起始点和结束点
    print(name.endswith('ce'))  #字符串以什么结尾。区分大小写
    print(name.expandtabs(30))  #设置table键的长度。
    print(name.find("e",2))     #返回找到的第一个所找字符的索引值,找不到返回-1.
    print(name2.format("George",26))    #字符串格式化
    print(name3.format(name = "George",age = 27))   #格式化输入
    print(name3.format_map({'name':'GEORGE','age':28}))
    print(name.index('W'))      #返回要查找的字符的索引值
    print('a1a'.isalnum())      #查看字符串是不是有数字和字符,取值范围是a-z,A-Z,0-9
    print('1111'.isdecimal())   #是不是一个正整数
    print('aaa'.isalpha())      #是不是字母
    print('a'.isidentifier())   #identifier关键字, 字符串是不是合法的变量名或关键字
    print('a'.islower())        #是不是小写
    print('A'.isupper())        #是不是大写
    print('22222'.isnumeric())  #是不是数字
    print('aaa'.isprintable())  #是不是可打印。
    print('a'.isspace())        #是不是空格
    print('a'.istitle())        #是不是英文标题  首字母大写
    print('||||'.join(['wang','george','mack']))   #将列表拼成字符串
    print(name3.ljust(50,'-'))  #左对齐,字符长度不够,‘-’来补
    print(name3.rjust(50,'-'))  #右对齐,字符长度不够,‘-’来补
    print(name3.lower())        #大写变小写
    print(name3.rfind('s'))     #从右边找字符
    print(name3.lstrip("My name is"))   #移除左边的字符串。
    print(name3.swapcase())     #大小写互换
    print(name3.replace('name',"NAME",1))   #替换,将name替换成NAME,替换一次
    print('Hello World'.zfill(17)) #定义字符串长度,不足用数字0补满。
    # -----------------------------------
    IN = 'anmefgori'
    OUT = '!@#$%^&*('
    
    trans_table = str.maketrans(IN,OUT)  #translate = 翻译
    print(name3.translate(trans_table))   #字符翻译
    # ------------------------------------
    print("hello %s,%s"%("sb","you"))
    print("hello %s, his age is %d"%("sb",35))
    print("hello %s, his age is %.4f"%("sb",35.53452345))
    print("hello {0}, his age is {1}".format("you",34))
    print("hello {0}, his age is {1}".format(34,"you"))
    print("hello {name}, his age is {age}".format(age=30,name="somebody"))
    print("hello {name}, his age is {age}".format_map({"name":"you","age":1000}))    #字典的形式

      查找: 

    #查找字符
    print("hello world".find("a",4))
    print("hello world".rfind("l"))
    print("hello world".index("q"))

      替换:

    #替换方法
    s="hello world"
    print(s.replace("world","Python"))  #完全匹配
    print(s)

     ---------- END ---------  

  • 相关阅读:
    java.lang.NoClassDefFoundError: org.junit.runner.Runner
    SpringMVC 八大注解
    spring @autowired使用总结
    Git使用入门
    N21_栈的压入弹出序列。
    N20_包含min函数的栈
    N19_顺时针打印指针
    N17_判断树B是不是树A的子结构
    N16_合并两个排序链表
    N15_反转链表后,输出新链表的表头。
  • 原文地址:https://www.cnblogs.com/george92/p/8495336.html
Copyright © 2011-2022 走看看