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 将所有的英文字符转化成大写
  • 相关阅读:
    面试笔试题目集
    [vs2010]:fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?
    [数据库] SQLite常见问题解答
    安卓学习资料总结39
    Android 学习资料总结40
    python变量的定义和使用
    python运算符
    python的注释
    print输出函数
    python数据类型转换
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9286276.html
Copyright © 2011-2022 走看看