Python下的字符串函数

来源:互联网 发布:java合成多张图片 编辑:程序博客网 时间:2024/06/06 07:34
1. 字符串长度函数 len

       len函数可以直接测得字符串的长度,使用语法如下:

[python] view plaincopy
  1. size = len(string)  
      举个小例子吧

[python] view plaincopy
  1. str1 = 'www.jeapedu.com'  
  2. size = len(str1)  
  3. index = 0  
  4. while index < size:  
  5.     print str1[index],  
  6.     index = index  + 1  
  7. print ""  

      2.字符串连接: +运算

      在python里通常将两个字串串连接通过“+”操作运算将两个字符串连接在一起。

[python] view plaincopy
  1. str1 = 'www.'  
  2. str2 = 'jeapedu.'  
  3. str3 = 'com'  
  4.   
  5. str4 = str1 + str2 + str3  
  6. print str4  
  7. str5 = str1,str2,str3  
  8. print str5  
      用+和用逗号连接多个字符串的结果是不一样的,读者可以看前述内容分析一下。


       3.字符串in运算和not in 运算

        语法结构:

[python] view plaincopy
  1. str1 in str2  
       例程如下:
[python] view plaincopy
  1. str1 = 'jeapedu'  
  2. str2 = 'www.jeapedu.com'  
  3. if str1 in str2:  
  4.      print str2  

       not in 和in的用法一样,这里我们就不再举例子了。

     4. 其他不太常用的函数

     4.1 判断类型的字符串函数

     4.1.1 判断是否都是有效字符字符串isalnum() 

     isalnum函数的作用是判断字符串里是否都是由字符、数字等常见有效字符的字符串。

    

[python] view plaincopy
  1. str1 = 'a1b2c3d4'  
  2. str2 = ',./;]'  
  3. str3 = ',a./;]'  
  4. str4 = ',6./;]'  
  5. t = str1.isalnum()  
  6. print t  
  7. f = str2.isalnum()  
  8. print f  
  9. t = str3.isalnum()  
  10. print t  
  11. f = str4.isalnum()  
  12. print f  
      运行结果如下:

        str1由字符和数字组成的字符串,不包含其他字符,故经isalnum函数检测后返回值为true,而str2里有一些其他字符组成的字符串故返回值false,str3和str4里尽管有字符或者数字,但含有非字符或者数字的字符故str3和str4的isalnum测试后的值为false,这个函数在网站注册时可以用于检测注册用户名是否合法。

       4.1.2判断是否都是数字和是否都是字符函数

       在python里可以调用isalpha和isdigit函数来判断字符串里是否都是字符或者数字组成。

[python] view plaincopy
  1. str1 = 'abcd'  
  2. str2 = '123'  
  3. str3 = '1a2c'  
  4. t = str1.isalpha()  
  5. print t  
  6. t = str2.isalpha()  
  7. print t  
  8. t = str1.isdigit()  
  9. print t  
  10. t = str2.isdigit()  
  11. print t  
  12. t = str3.isdigit()  
  13. print t  
  14. t = str3.isdigit()  
  15. print t  
  16. t = str3.isalnum()  
  17. print t  
       运行结果如下所示:

      
       4.1.3判断是否都是大小写      
       islower函数可以判断字符串里的所有字符是否都是小写?isupper函数则是判断字符串里的字符是否都是大写的?

[python] view plaincopy
  1. str1 = 'abcd1234'  
  2. str2 = 'abcd'  
  3. str3 = 'ABCD'  
  4. str4 = '123abcD'  
  5. t = str1.islower()  
  6. print t  
  7. t = str2.islower()  
  8. print t  
  9. t = str3.islower()  
  10. print t  
  11. t = str3.isupper()  
  12. print t  
  13. t = str4.islower()  
  14. print t  
  15. t = str4.isupper()  
  16. print t  
           运行结果如下:

      4.1.4 判断是否有空格函数

      isspace函数可以判断字符串里是有只含有空格、\n、\t的等广义空格?

[python] view plaincopy
  1. str1 = 'a1 b2c'  
  2. str2 = ' aaa'  
  3. str3 = 'a\nb'  
  4. str4 = '\ta'  
  5. str5 = '\n'  
  6. str6 = ' '  
  7. str7 = '\n'  
  8. t = str1.isspace()  
  9. print t  
  10. t = str2.isspace()  
  11. print t  
  12. t = str3.isspace()  
  13. print t  
  14. t = str4.isspace()  
  15. print t  
  16. t = str5.isspace()  
  17. print t  
  18. t = str6.isspace()  
  19. print t  
  20. t = str7.isspace()  
  21. print t  
       运行结果如下所示:


       判空函数isspace可以用于检查用户是否已输入。 

           4.1.5 大小写转换函数lower和upper

       这两个函数可以将大写转小写或者相反,但此两函数并未修改原值,只是原值的拷贝数据变成了全部都是小写或者全部都是大写。

[python] view plaincopy
  1. str1 = 'abc'  
  2. str2 = str1.upper()  
  3. print str1  
  4. print str2  
  5. str3 = 'aBcdE'  
  6. str4 = str3.upper()  
  7. print str3  
  8. print str4  
  9. str5 = str3.upper()  
  10. print str5  
     运行结果是:

[python] view plaincopy
  1. abc  
  2. ABC  
  3. aBcdE  
  4. ABCDE  
  5. ABCDE  

      由上边的例子可以看出,1)这两个函数将字符串里的大写或者小写转换成小写或者大写字母。2)str3原值未发生变化,str4、str5是str3的一份拷贝后修改数据。
      4.2 strip函数

      strip是去除某字符(通常是广义空格:空格、\n、\ t 等)的函数,有两个变种lstrip和rstrip,这两个函数可以去除字符串最开始和最尾部的所有空格(不止一个)。

[python] view plaincopy
  1. str1 = '  a b  c  '  
  2. print str1  
  3. str2 = str1.lstrip()  
  4. print str2  
  5. str3 = str2.rstrip()  
  6. print str3  
     代码第3行去除了str1字符串字母‘c’后边的空格,代码第5行去除了str1的最开始字母‘a’前边的空格,这两个函数在数据库写入用户输入信息时可以去除多余的空格,保留有效信息到数据库里。
     strip函数不仅可以去除空格,而且还可以去除指定字符。

    

[python] view plaincopy
  1. str1 = '  a b a  c c a b '  
  2. print str1  
  3. str2 = str1.lstrip()  
  4. print str2  
  5. str3 = str2.rstrip()  
  6. print str3  
  7. str4 = str3.lstrip('a')  
  8. print str4  
  9. str5 = str4.rstrip('c')  
  10. print str5  
  11. str6 = str4.rstrip('b')  
  12. print str6  
       代码第7行去除了字符串里的‘a’,代码第9行并没有去掉字符串里最右侧的‘c’,原因在于strip函数只能去除尾部的字符,‘c’字符没有在字符串str4尾部,而代码第11行去掉了str4尾部的‘b’这个字符,运行结果如下所示:  

[python] view plaincopy
  1.   a b a  c c a b   
  2. a b a  c c a b   
  3. a b a  c c a b  
  4.  b a  c c a b  
  5.  b a  c c a b  
  6.  b a  c c a   
      4.3 字符串的查找和替换函数

      Python提供了丰富的字符串相关库函数,其中find和replace函数可以实现子字符串的查找和替换功能。在举例使用这两个函数前,我们先看看startswith和endswith这两个函数是判断一个字符串是否以某字符串开始或者结束的。

[python]view plaincopy
  1. s1 = ".com"  
  2. s2 = ".cn"  
  3. s3 = "www."  
  4. s4 = "www.jeapedu.com"  
  5.   
  6. if s4.startswith(s3):  
  7.   print "www"  
  8. if s4.endswith(s1):  
  9.   print ".com"  
      运行结果如下:

[python] view plaincopy
  1. www  
  2. .com  
    好,有了对startswith和endswith函数的理解我们再看看find函数和replace的使用方法。
[python] view plaincopy
  1. # -*- coding: utf-8 -*-  
  2. s1 = ".com"  
  3. s3 = "www."  
  4. s4 = "www.jeapedu.com"  
  5.   
  6. if s4.startswith(s3):  
  7.   print "www"  
  8. if s4.endswith(s1):  
  9.   print ".com"  
  10. if s4.find("jeapedu"):  
  11.   s4 = s4.replace("jeapedu""智普教育")  
  12.   print s4  

      replace函数通常有两个参数,第一个参数是被替换的字符串,第二个参数是替换成的字符串。

    

    4.4 join函数

    join是string类型的一个函数,用调用他的字符串去连接参数里的列表   ‘,'.join调用者是',',python里面万物皆对象,','是一个string类型的对象,调用join函数,将后面的列表里的值用逗号连接成新的字符串;

     4.5 split函数

         split是一个非常重要的字符串函数,是join的逆方法,用来将字符串分割,返回一个字符串列表:

        

     函数的参数是分隔符,如果不提供任何分隔符,将以空格作为分隔符(空格,制表,换行等)