Python字符串在实际中的操作手册

开发 后端
Python字符串如何进行相关环境的融合与使用。这个问题需要大家进行学习相关的知识。下面我们就详细的看看相关的技术问题。

Python字符串在使用的时候是需要我们进行详细的学习。下面我们就看看在具体的操作中如何才能更好的进行这个字符串的使用方法。在之后的使用中还是需要我们继续学习。

  1. #coding:utf-8   
  2. #两个小函数  
  3. #一、查找字符在字符串中第一次出现的位置.   
  4. def find(string, char):   
  5. index = 0   
  6. while index < len(string):   
  7. if (string[index] == char):   
  8. return index   
  9. index += 11  
  10. return -111   
  11. #二、查找字符在字符串中的总数  
  12. def findSum(string, char):  
  13. index = 0 
  14. count = 0 
  15. while index < len(string):  
  16. if (string[index] == char):  
  17. count += 1  
  18. index += 1  
  19. return count  
  20. #使用以上两个函数  
  21. print "字符1在字符串1211211234中第一次出现的位置: ", find("1211
    211234", "1")  
  22. print "字符1在字符串1211211234中出现的次数:", findSum("1211211
    234", "1")  
  23. import string #引入string库  
  24. print string.find('www.cctv.com', 'com') #result=9 
  25.  print string.find('Good','d') #result = 3 
  26. print string.find('canada', 'a',2,9) #result =3,用法如下:  
  27. #string.find(s, sub[, start[, end]])函数说明  
  28. #Return the lowest index in s where the substring sub is 
    found such that sub is  
  29. #wholly contained in s[start:end]. Return -1 on failure. 
    Defaults for start and  
  30. #end and interpretation of negative values is the same 
    as for slices.  
  31. print string.lowercase #常量,abcdefghijklmnopqrstuvwxyz  
  32. print string.uppercase #常量,ABCDEFGHIJKLMNOPQRSTUVWXYZ  
  33. print string.digits #常量,012345678937   
  34. def isLower(char): #判断一个字符是否为小写  
  35. if (string.find(string.lowercase, char) > -1):  
  36. return 'Good'  
  37. return 'bad'  
  38. print isLower('S')  
  39. def isLowertest(char): #另外一种判断字符是否为小写的方法  
  40. return char in string.lowercase46 print isLowertest('a')  

以上就是对Python字符串的相关介绍,希望大家有所收获。

【编辑推荐】

  1. 探究Python性能为什么如此的强大
  2. Python编程语言在未来的发展趋势
  3. Python多线程具体运用的方法
  4. Python编程语言的发展历程介绍
  5. Python编程语言与Zpoe之间不解的情缘
责任编辑:张浩 来源: 博客园
相关推荐

2010-03-18 17:23:55

Java Socket

2010-03-11 18:21:43

Stackless P

2009-12-22 17:24:14

Linux配置Dual

2009-12-01 14:43:28

安装RDAC驱动

2009-12-02 15:57:26

Linux系统

2009-12-17 16:10:24

Linux系统安装声卡

2010-03-16 10:58:35

Python字符串

2010-03-16 16:22:36

Python字符串

2009-12-23 16:29:01

Linux操作系统

2010-06-01 17:45:45

SVN 基本操作

2010-06-17 16:22:04

SQL Server

2010-03-22 18:53:53

Python格式化字符

2020-09-24 10:45:52

计算机互联网 技术

2010-03-22 18:20:47

Python正则表达式

2010-04-15 16:47:46

Oracle字段

2010-06-07 16:36:29

MySQL连接字符串

2023-09-26 19:45:47

2010-03-03 16:25:41

Python字符串显示

2010-06-17 16:38:14

MS SQL Serv

2010-09-06 17:30:46

SQL函数
点赞
收藏

51CTO技术栈公众号