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

    #-*- coding:utf-8 -*-
    
    strword = "i will fly with you , fly on the sky ."
    
    #find
    print(strword.find("fly"))   #打印7
    
    #find返回找到第一个字符串的下标,找不到返回-1
    
    print("==rfind==")
    
    #rfind
    print(strword.rfind("fly"))
    #rfind 从后向前查找,找到返回下标,找不到返回-1
    
    print("==index==")
    
    #index
    print(strword.index("fly")) 
    #index 找到返回首字符的下标,找不到报错ValueError: substring not found
    
    print("==rindex==")
    #rindex
    print(strword.rindex("fly"))
    #功能与rfind类似,返回值不同
    
    print("==count==")
    #count
    print(strword.count("fly"))
    #count 返回找到字符串的个数,找不到返回0
    
    print("==replace==")
    #replace
    print(strword.replace("fly","xxx"))
    #全文替换指定字符串,由于字符串属于不可变类型,所以replace()函数不会修改strword的值
    #replace扩展 可以指定替换多少个,但是替换的顺序还是从前向后的
    print(strword.replace("fly","xxx",1))
    
    print("===split==")
    #split
    print(strword.split(" "))
    #split 切割函数,按照指定字符切割字符串
    
    word = "hello world"
    
    print("==capitalize==")
    #capitalize
    print(word.capitalize())
    #字符串首单词的第一个字符大写
    
    print("==title==")
    print(word.title())
    #title 字符串中每个单词首字母大写
    
    print("==startswith==")
    print(word.startswith("he"))
    #startswith 是否以某字符串开头,是返回true,不是返回flase
    
    print("==endswith==")
    print(word.endswith("ld"))
    #endswith 判断以某字符串结尾,是返回true,不是返回flase
    
    nword = "A B C a b c"
    print("==lower==")
    print(nword.lower())
    #lower 将所有英文字符转化成小写
    
    
    print("==upper==")
    #upper
    print(nword.upper())
    #upper 将所有的英文字符转化成大写
  • 相关阅读:
    POJ 3295 spfa判断是否存在负权回路
    hdu 1385 最短路+输出字典序好题
    hdu 1143 递归
    hdu 1598 并查集+贪心其实就是kruskal的思想
    快速排序
    HDU:Fighting for HDU
    HDU:Saving HDU
    HDU:悼念512汶川大地震遇难同胞——老人是真饿了
    每日一题统计指定的字符串
    每日一题2求一个文件里面指定字符出现的次数
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9286276.html
Copyright © 2011-2022 走看看